在数组对象中组合键值

时间:2019-03-01 07:37:05

标签: javascript arrays

我希望将所有键值组合到此数组的对象中。

var currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];

最终结果应为:

[
  ['Brunello Cucinelli', 'tasselled black low-top lace-up', 1000],
  ['Brunello Cucinelli', 'tasselled green low-top lace-up', 1100],
  // ...
]

我有以下代码:

function renderInventory(inventory) {
  const arr = [];
  for (var i = 0; i < inventory.length; i++) {
    for (var n = 0; n < inventory[i].shoes.length; n++) {
      arr.push([inventory[i].name + ', ' + inventory[i].shoes[n].name + ', ' + inventory[i].shoes[n].price]);
    }
  }
  return arr;
}

它给了我以下结果:

[
  ['Brunello Cucinelli, tasselled black low-top lace-up, 1000'],
  ['Brunello Cucinelli, tasselled green low-top lace-up, 1100'],
...
]

我不确定该怎么办,引号环绕每个元素而不是整个数组。

4 个答案:

答案 0 :(得分:3)

您可以使用flatMap来获取每个外部对象,并从每个对象中提取name和映射的shoes数组:

var currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
];
const output = currentInventory.flatMap(
  ({ name, shoes }) => shoes.map(
    shoe => [name, shoe.name, shoe.price]
  )
);
console.log(output);
<script src="https://polyfill.io/v3/polyfill.min.js?features=default%2CArray.prototype.flatMap"></script>

答案 1 :(得分:3)

您只需要返回一个包含项的数组,而不是一个连接字符串。

arr.push([inventory[i].name, inventory[i].shoes[n].name,  inventory[i].shoes[n].price]);

function renderInventory(inventory) {
  const arr = [];
  for (var i = 0; i < inventory.length; i++) {
    for (var n = 0; n < inventory[i].shoes.length; n++) {
      arr.push([inventory[i].name, inventory[i].shoes[n].name,  inventory[i].shoes[n].price]);
    }
  }
  return arr;
}

var currentInventory = [{ name: 'Brunello Cucinelli', shoes: [{ name: 'tasselled black low-top lace-up', price: 1000 }, { name: 'tasselled green low-top lace-up', price: 1100 }, { name: 'plain beige suede moccasin', price: 950 }, { name: 'plain olive suede moccasin', price: 1050 }] }, { name: 'Gucci', shoes: [{ name: 'red leather laced sneakers', price: 800 }, { name: 'black leather laced sneakers', price: 900 }] }];

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

采用解构后的值的较短方法。

function renderInventory(inventory) {
    return inventory.reduce((r, { name: item, shoes }) => [
        ...r,
        ...shoes.map(({ name, price }) => [item, name, price])
    ], []);
}

var currentInventory = [{ name: 'Brunello Cucinelli', shoes: [{ name: 'tasselled black low-top lace-up', price: 1000 }, { name: 'tasselled green low-top lace-up', price: 1100 }, { name: 'plain beige suede moccasin', price: 950 }, { name: 'plain olive suede moccasin', price: 1050 }] }, { name: 'Gucci', shoes: [{ name: 'red leather laced sneakers', price: 800 }, { name: 'black leather laced sneakers', price: 900 }] }];

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

答案 2 :(得分:0)

Array.prototype.reduce()Array.prototype.map()Array.prototype.concat()

const currentInventory = [{name: 'Brunello Cucinelli',shoes: [{name: 'tasselled black low-top lace-up', price: 1000},{name: 'tasselled green low-top lace-up', price: 1100},{name: 'plain beige suede moccasin', price: 950},{name: 'plain olive suede moccasin', price: 1050}]},{name: 'Gucci',shoes: [{name: 'red leather laced sneakers', price: 800},{name: 'black leather laced sneakers', price: 900}]}];
const result = currentInventory.reduce((a, c) => [].concat(a, c.shoes.map(s => ([c.name, s.name, s.price]))), [])

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

答案 3 :(得分:0)

首先,您需要收集子阵列鞋并将其放平。然后使用map函数构造所需的方式。

代码在这里

const currentInventory = [
  {
    name: "Brunello Cucinelli",
    shoes: [
      { name: "tasselled black low-top lace-up", price: 1000 },
      { name: "tasselled green low-top lace-up", price: 1100 },
      { name: "plain beige suede moccasin", price: 950 },
      { name: "plain olive suede moccasin", price: 1050 }
    ]
  },
  {
    name: "Gucci",
    shoes: [
      { name: "red leather laced sneakers", price: 800 },
      { name: "black leather laced sneakers", price: 900 }
    ]
  }
];

const flatten = currentInventory
  .reduce((flat, inventory) => {
    flat.push(inventory.shoes);
    return flat;
  }, [])
  .flat();

const result = flatten.map(({ name, price }) => {
  return [name, price];
});

console.log(result);

短版

const result = currentInventory
  .reduce((flat, { shoes }) => {
    flat.push(shoes);
    return flat;
  }, [])
  .flat()
  .map(({ name, price }) => [name, price]);