【问题标题】:Iterate Nested Value in Array in JavaScript/ES6/ES7在 JavaScript/ES6/ES7 中迭代数组中的嵌套值
【发布时间】:2021-04-05 07:32:05
【问题描述】:

我需要在我的 javascript 中迭代一个嵌套值。

我想要的输出应该是这样的

shows: ['food.order', 'drink.order', 'play.basketball', 'play.soccer']

const results = [
  {
    "ID": "shops",
    "Shopping": [
      {
        "ID": "food.order",
        "Name": "Food"
      },
      {
        "ID": "drink.order",
        "Name": "Drink"
      }
    ]
  },
  {
    "ID": "fun",
    "Sports": [
      {
        "ID": "play.basketball",
        "Name": "Basketball"
      },
      {
        "ID": "play.soccer",
        "Name": "Soccer"
      },
    ]
  }
];

console.log(results);

const final = { shows: results.map(data => data['key'].ID) }

【问题讨论】:

  • 您的对象中除了ShoppingSports 之外还有哪些属性名称?
  • @Bergi。任何东西,它的动态
  • 但绝不是ID,对吧?一个对象中可以有多个这样的键吗?

标签: javascript ecmascript-6 ecmascript-2016


【解决方案1】:

虽然您的问题并不清楚,但我假设您正在搜索 ID 属性并想要获取 ID 的值并创建一个数组。你可以试试这个方法-

const results = [{"ID": "shops", "Shopping": [{ "ID": "food.order", "Name": "Food"},{ "ID": "drink.order", "Name": "Drink"}]},{"ID": "fun", "Sports": [{ "ID": "play.basketball", "Name": "Basketball"},{ "ID": "play.soccer", "Name": "Soccer"}]}];

const ans = results.reduce((acc, item) => {

  // Iterate throw the each item's properties
  Object.values(item).forEach(val => {
    
      // Filter out the objects which has the `ID` property and get the value of the `ID`.
      const ids = typeof val === 'object' && val instanceof Array
        ? val.filter(x => x.ID !== undefined).map(({ID}) => ID)
        : [];

      acc = [...acc, ...ids];
    
  });

  return acc;
}, []);

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

【讨论】:

  • 是否也有可能,我只会得到每个数组的第一个值。像这样? ["food.order", "play.basketball"]
  • 是的!有可能,如果您将行 acc = [...acc, ...ids] 替换为 acc = ids.length ? [...acc, ids[0]] : acc;
【解决方案2】:

你在寻找这样的东西吗?

const results = [{"ID": "shops", "Shopping": [{ "ID": "food.order", "Name": "Food"},{ "ID": "drink.order", "Name": "Drink"}]},{"ID": "fun", "Sports": [{ "ID": "play.basketball", "Name": "Basketball"},{ "ID": "play.soccer", "Name": "Soccer"}]}];

const final = results.reduce((p, n) => {
  // Get all object's array props and then reduce their keys
  const mapped = Object.keys(n).filter((key) => Array.isArray(n[key])).reduce((arr, key) => [
      ...arr,
      // Get the array by using the object's key, filter out all objects which don't have
      // an 'ID' key, and return a new array which only contains the x.ID property
      ...n[key].filter((x) => x.ID).map((x) => x.ID)
  ], []);
  
  return [
    ...p,
    ...mapped,
  ];
}, []);

console.log('final', final);

【讨论】:

    【解决方案3】:

    const results=[{ID:"shops",Shopping:[{ID:"food.order",Name:"Food"},{ID:"drink.order",Name:"Drink"}]},{ID:"fun",Sports:[{ID:"play.basketball",Name:"Basketball"},{ID:"play.soccer",Name:"Soccer"}]}];
    
    
    let arr = results.flatMap(e => Object.values(e).filter(n => Array.isArray(n))) // at this stage you have an array of arrays
             .flat() // at this stage you have flat array from previous stage
             .map(s => s.ID) // Now you pick the IDs
    
    console.log(arr)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2019-10-12
      相关资源
      最近更新 更多