1

我有的:

myArray = [ {type: "My Application"}, {type: "My Component"}, {color: ["red"] } ]

我需要的:

withUniqueKeys = [ {type: ["My Application", "My Component"] }, {color: ["red"]} ]

我将如何遍历 myArray 以获取像 withUniquKeys 这样的数组?我玩这个 WAAAYYYY 太久了。一个 lodash 解决方案也可以。

4

5 回答 5

4

您可以使用reduceObject.entries

let myArray = [ {type: "My Application"}, {type: "My Component"}, {color: "red" } ]

let op = myArray.reduce((op,inp)=>{
  let [key,value] = Object.entries(inp)[0]
  op[key] = op[key] || []
  op[key].push(value)
  return op
},{})

// in case you want property with one value to be object only

let final = Object.entries(op)
           .map(([key,value]) => ( {[key]: value.length === 1 ? value[0] : value}))

console.log(final)

IMO最好让你的数据结构保持一致,这样就可以很容易地用于以后的目的,否则你需要检查该值是否只是一个字符串或数组,而不是应用方法

let myArray = [ {type: "My Application"}, {type: "My Component"}, {color: "red" } ]

let op = myArray.reduce((op,inp)=>{
  let [key,value] = Object.entries(inp)[0]
  op[key] = op[key] || []
  op[key].push(value)
  return op
},{})

console.log(op)

于 2019-04-05T18:24:03.800 回答
1

尝试这个:

  1. 减少初始数组以按键对条目进行分组
  2. 将对象条目映射到相应对象的数组

let myArray = [ {type: "My Application"}, {type: "My Component"}, {color: ["red"] } ]

myArray = myArray.reduce((acc, el) => {
  let prop = Object.keys(el)[0];
  if (!acc[prop]) acc[prop] = [el[prop]];
  else acc[prop].push(el[prop])
  return acc;
},{})

myArray = Object.keys(myArray).map(d => ({[d]:myArray[d].length ===1?myArray[d][0]:myArray[d]}));

console.log(myArray)

于 2019-04-05T18:32:10.490 回答
1

你可以像这样使用Array.prototype.reduce()Object.entries()来做到这一点:

const arr = [{type: "My Application"}, {type: "My Component"}, {color: "red" }];

const result = Object.entries(arr.reduce((acc, x) => {
  Object.entries(x).forEach(([k, v]) => {
    acc[k] = [...(acc[k] || []), v];
  });
  return acc;
}, {})).map(([k, v]) => ({ [k]: v.length > 1 ? v : v[0] }));

console.log(result);

于 2019-04-05T18:35:51.583 回答
0

首先,您可以使用Array.reduce()keys. 然后,在第二步中,您可以在生成的Object.entries()上使用Array.map( )来获得所需的结构:

let myArray = [
  {type: "My Application", category: "game"},
  {type: "My Component", category: "other"},
  {color: "red" }
];

let res = myArray.reduce((acc, curr) =>
{
    Object.keys(curr).forEach(k =>
    {
        acc[k] = (acc[k] || []).concat(curr[k]);
    });
    return acc;
}, {});

res = Object.entries(res).map(([k, v]) => ({[k]: v.length > 1 ? v : v[0]}));

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

请注意,如果您的输入对象有超过一对key/value.

于 2019-04-05T18:34:06.197 回答
0

其他答案看起来不错,这是一个简短的替代方案,也使用Array.reduceand Object.entries

const myArray = [{type: "My Application"}, {type: "My Component"}, {color: "red"}];

const withUniqueKeys = Object.entries(
  myArray.reduce((result, item) => {
    const [key, val] = Object.entries(item)[0];
    result[key] = result[key] ? [...[result[key]], val] : val;
    return result;
  }, {})
).map(([key, val]) => ({[key]: val}));

console.log(withUniqueKeys);

于 2019-04-05T18:36:12.713 回答