使用相同的键分组,并用逗号连接值

时间:2019-03-21 11:13:10

标签: javascript arrays dictionary reduce

我有以下带有动态键/值对的示例数据,我需要使用相同的键将它们分组,并用逗号将多个值连接起来。我能够提取数组上的唯一键,然后卡住

输入

[
          {
            "c1": "USA"
          },
          {
            "c2": "Korea"
          },
          {
            "c4": "japan"
          },
          {
            "c3": "india"
          },
          {
            "c1": "australia"
          },
          {
            "c2": "france"
          }
        ]

输出

[
          {
            "c1": "USA,australia",
            "c2": "Korea,france",
            "c4": "japan",
            "c3": "india"
          }
        ]

let data=[
      {
        "c1": "USA"
      },
      {
        "c2": "Korea"
      },
      {
        "c4": "japan"
      },
      {
        "c3": "india"
      },
      {
        "c1": "australia"
      },
      {
        "c2": "france"
      }
    ]
    
    var output = Object.keys(data).map(element => {
    
      var ret = Object.keys(data[element]);
      
      return ret;
    
    })
    let c=[...new Set(output.flat())];
    console.log(c);

如何从我得到的唯一数组中以逗号连接值,我想我已经中途了

5 个答案:

答案 0 :(得分:1)

您需要在map()函数的帮助下输出新的组织对象。

没有检查代码-但它应该看起来像这样。

 let dataNew = {};
 var output = Object.keys(data).map(element => {
      if (typeof( dataNew[element] ) === 'undefined') {
        dataNew[element] = data[element]
      } else {
        dataNew[element] += `,${data[element]}`;
      }
      return dataNew;
    })

像这样,您正在创建一个具有统一值逗号的新对象。

答案 1 :(得分:0)

对不起,我回过头去了最后一个阵列的重复数据。

const data = [
  {
    "c1": "USA"
  },
  {
    "c2": "Korea"
  },
  {
    "c4": "japan"
  },
  {
    "c3": "india"
  },
  {
    "c1": "australia"
  },
  {
    "c2": "france"
  }
]

let endData = [{}]

data.forEach(d => {
  const [dataKey] = Object.keys(d)
  const relatedValues = data.map(d => dataKey in d ? d[dataKey] : null).filter(Boolean)
  const combinedValues = [d[dataKey], ...relatedValues]
  
  const unique = endData.find(d => Object.keys(d)[0] === dataKey) === undefined
  if (unique) endData[0][dataKey] = `${[...new Set(combinedValues)]}`
})

console.log(endData)

答案 2 :(得分:0)

您可以尝试Array.reduce原型

let data=[
      {
        "c1": "USA"
      },
      {
        "c2": "Korea"
      },
      {
        "c4": "japan"
      },
      {
        "c3": "india"
      },
      {
        "c1": "australia"
      },
      {
        "c2": "france"
      }
    ]

const combinedData = data.reduce((memory, current) => {
	const currentKeys = Object.keys(current);
	const found = memory.find(m => 
		Object.keys(m).some(key => currentKeys.includes(key))
	);

	if (found) {
		const key = Object.keys(found).pop();
		found[key] = `${found[key]},${current[key]}`;
    } else {
		memory.push(current);
    }

	return memory;

}, []);

console.log(combinedData);

答案 3 :(得分:0)

获取键值对,将它们展平(因为键是否在同一对象中无关紧要),然后使用另一个对象进行合并:

 const result = input
  .map(Object.entries)
  .flat()
  .reduce((obj, [k, v]) => ((obj[k] = obj[k] ? (obj[k] + ", " + v) : v), obj), {});

答案 4 :(得分:0)

不更改代码: 遍历Set c的每个值,然后过滤具有相同键的数据项,然后将其与join (',')合并。

let data=[
      {
        "c1": "USA"
      },
      {
        "c2": "Korea"
      },
      {
        "c4": "japan"
      },
      {
        "c3": "india"
      },
      {
        "c1": "australia"
      },
      {
        "c2": "france"
      }
    ]
    
    var output = Object.keys(data).map(element => {
    
      var ret = Object.keys(data[element]);
      
      return ret;
    
    })
    let c=[...new Set(output.flat())];
    
    /* Join equals */
    const n = []; 
    c.forEach( (it, idx) => {
      n.push({});
      n[idx][it] = data.filter(its => it === Object.keys(its)[0])
                  .map( its => Object.values(its)[0])
                  .join(',');
    })
    console.log(n);