Как преобразовать массив в объект с "n" массивами? - PullRequest
1 голос
/ 10 июня 2019

Добрый день,

У меня есть два массива.Один с всеми значениями , а другой с идентификаторами , который я хочу отфильтровать в первом массиве и отделить значения в объекте.

Пример:

Массив со всеми значениями

const ArrayTodosOsValores = [
  {
    "ric": "B3SA3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "B3 BRASIL BALCAO ORD BAG",
    "ticker": "B3SA3",
    "isin": "BRB3SAACNOR6",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "BBAS3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "BANCO DO BRASIL ORD",
    "ticker": "BBAS3",
    "isin": "BRBBASACNOR3",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "1ZEG1",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB1",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "1ZEG9",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB9",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
]

Массив идентификаторов


const arrayIdentifiers = ["0 # .BVSP", "0 # 1ZE:"]

Как сделать ArrayAllSettings для фильтрации через свойство ricRoot со значениями arrayIdentifiers , что приводит кследующий объект:

const result = {
  0 # .BVSP: [
  {
    "ric": "B3SA3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "B3 BRASIL BALCAO ORD BAG",
    "ticker": "B3SA3",
    "isin": "BRB3SAACNOR6",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "BBAS3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "BANCO DO BRASIL ORD",
    "ticker": "BBAS3",
    "isin": "BRBBASACNOR3",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  }],
  0 # 1ZE: [
  {
    "ric": "1ZEG1",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB1",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "1ZEG9",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB9",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
 ]
}

Как я могу это сделать?Спасибо.

Ответы [ 2 ]

2 голосов
/ 10 июня 2019

Вы можете создавать новые свойства, фильтруя значения. С (в ближайшее время) Object.fromEntries

var array = [{ ric: "B3SA3.SA", category: 3, categoryString: "Equity", arcCode: "", description: "B3 BRASIL BALCAO ORD BAG", ticker: "B3SA3", isin: "BRB3SAACNOR6", cusip: "", exchange: "BOV", country: "BR", instrType: 12, ricRoot: "0 # .BVSP", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "BBAS3.SA", category: 3, categoryString: "Equity", arcCode: "", description: "BANCO DO BRASIL ORD", ticker: "BBAS3", isin: "BRBBASACNOR3", cusip: "", exchange: "BOV", country: "BR", instrType: 12, ricRoot: "0 # .BVSP", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "1ZEG1", category: 1, categoryString: "Derivatives", arcCode: "", description: "ETHANOL FEB1", ticker: "EH", isin: "", cusip: "", exchange: "CBT", country: "US", instrType: 9, ricRoot: "0 # 1ZE:", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "1ZEG9", category: 1, categoryString: "Derivatives", arcCode: "", description: "ETHANOL FEB9", ticker: "EH", isin: "", cusip: "", exchange: "CBT", country: "US", instrType: 9, ricRoot: "0 # 1ZE:", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }],
    identifiers = ["0 # .BVSP", "0 # 1ZE:"],
    result = Object.fromEntries(
        identifiers.map(k => [k, array.filter(o => Object.values(o).includes(k))])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

С Object.assign

var array = [{ ric: "B3SA3.SA", category: 3, categoryString: "Equity", arcCode: "", description: "B3 BRASIL BALCAO ORD BAG", ticker: "B3SA3", isin: "BRB3SAACNOR6", cusip: "", exchange: "BOV", country: "BR", instrType: 12, ricRoot: "0 # .BVSP", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "BBAS3.SA", category: 3, categoryString: "Equity", arcCode: "", description: "BANCO DO BRASIL ORD", ticker: "BBAS3", isin: "BRBBASACNOR3", cusip: "", exchange: "BOV", country: "BR", instrType: 12, ricRoot: "0 # .BVSP", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "1ZEG1", category: 1, categoryString: "Derivatives", arcCode: "", description: "ETHANOL FEB1", ticker: "EH", isin: "", cusip: "", exchange: "CBT", country: "US", instrType: 9, ricRoot: "0 # 1ZE:", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }, { ric: "1ZEG9", category: 1, categoryString: "Derivatives", arcCode: "", description: "ETHANOL FEB9", ticker: "EH", isin: "", cusip: "", exchange: "CBT", country: "US", instrType: 9, ricRoot: "0 # 1ZE:", hasChain: true, useChain: false, identifierTypeFound: "Ric", historyMonths: 3 }],
    identifiers = ["0 # .BVSP", "0 # 1ZE:"],
    result = Object.assign(
        {},
        ...identifiers.map(k => ({ [k]: array.filter(o => Object.values(o).includes(k)) }))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1 голос
/ 10 июня 2019

Вы можете сделать это, используя функцию redu () и фильтруя исходные массивы следующим образом:

const ArrayTodosOsValores = [
  {
    "ric": "B3SA3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "B3 BRASIL BALCAO ORD BAG",
    "ticker": "B3SA3",
    "isin": "BRB3SAACNOR6",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "BBAS3.SA",
    "category": 3,
    "categoryString": "Equity",
    "arcCode": "",
    "description": "BANCO DO BRASIL ORD",
    "ticker": "BBAS3",
    "isin": "BRBBASACNOR3",
    "cusip": "",
    "exchange": "BOV",
    "country": "BR",
    "instrType": 12,
    "ricRoot": "0 # .BVSP",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "1ZEG1",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB1",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
  {
    "ric": "1ZEG9",
    "category": 1,
    "categoryString": "Derivatives",
    "arcCode": "",
    "description": "ETHANOL FEB9",
    "ticker": "EH",
    "isin": "",
    "cusip": "",
    "exchange": "CBT",
    "country": "US",
    "instrType": 9,
    "ricRoot": "0 # 1ZE:",
    "hasChain": true,
    "useChain": false,
    "identifierTypeFound": "Ric",
    "historyMonths": 3
  },
];

const arrayIdentifiers = ["0 # .BVSP", "0 # 1ZE:"]

const result = arrayIdentifiers.reduce((acc, arrayIdentifier) => {
  acc[arrayIdentifiers] = ArrayTodosOsValores.filter(arr => arr.ricRoot === arrayIdentifier);
  return acc;
}, {});

console.log(result);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...