简体   繁体   English

根据相似的值将对象拆分为对象数组

[英]Split an object in to array of objects based on similar values

var pr = {
    name: "ball",
    race: "ball",
    weapon: "axe",
};

var save=new Object;
var keys=Object.keys(pr);

for(var k in pr) { 

}
console.log(save); // should end up with {name:'ball',race:'ball'}

If I have understood the question correctly, one option is: 如果我正确理解了这个问题,则可以选择以下一种方法:

const keys = Object.keys(pr);
const ret = keys.reduce((ret, k, i) => {
  const f = keys.find((k2, i2) => i !== i2 && pr[k] === pr[k2]);
  if (f) ret[k] = pr[k];
  return ret;
}, {});

Here is what I came up with. 这是我想出的。

var pr = {
  name: "ball",
  race: "ball",
  weapon: "axe"
};

const dupValues = Object.values(pr).reduce(
  (acc, cur) => ({ ...acc, [cur]: (acc[cur] || 0) + 1 }),
  {}
);

const result = Object.keys(pr)
  .filter(key => dupValues[pr[key]] > 1)
  .reduce((acc, curr) => ({ ...acc, [curr]: pr[curr] }), {});

console.log(result);
// {name:'ball',race:'ball'}

One way to do it is use the save object as a histogram, keeping track of duplicates. 一种方法是将save对象用作直方图,并跟踪重复项。 Then, filter out any keys with 0 count using reduce . 然后,使用reduce过滤掉任何计数为0的键。 This should have better performance than a linear function like find : 它应该比线性函数(如find具有更好的性能:

 var pr = { name: "ball", race: "ball", weapon: "axe" }; var save = {}; for (var k in pr) { save[pr[k]] = pr[k] in save ? save[pr[k]] + 1 : 0; } var result = Object.keys(pr).reduce((a, e) => { if (save[pr[e]]) { a[e] = pr[e]; } return a; }, {}); console.log(result); 

It works. 有用。 Simple and clear. 简单明了。 References : Array.reduce() 参考: Array.reduce()

Iterate through each key value pair, and accumulate the result until the loop ends. 遍历每个键值对,并累积结果,直到循环结束。

 var pr = { name: "ball", race: "ball", weapon: "axe", item:"bat", newitem:"bat", newweapon: "axe" }; var result = Object.keys(pr).reduce(function(acc, key){ var ispresent = false; acc.forEach(function(obj,i){ if(ispresent) return; if(Object.values(obj)[0]===pr[key]) { obj[key]=pr[key]; ispresent = true; } }); if(!ispresent) { var newobj = {};newobj[key]=pr[key]; acc.push(newobj) } return acc; },[]) console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据JavaScript中的条件将对象拆分为对象数组 - Split an object into array of objects based on a condition in JavaScript 我想根据相似对象数组中的键动态创建对象值数组 - I want to dynamically create arrays of object values based on their keys from an array of similar objects 如何根据其中一个值中的数据将对象拆分为多个对象[数组中的唯一值] - How to split an object into multiple objects based on data in one of it's values[unique values in an array] 从基于另一个数组的对象数组中删除不相似的值 - remove non similar values from an array of objects based on another array 基于相似对象值匹配 json 对象时出错 - Errors while matching json objects based on similar object values 如何根据相似的 id 从 object 数组中提取对象 - how to extract objects from array of object based on similar id 如何根据对象的值将对象数组拆分为另一个数组? - How to split an Array of Objects into another Arrays based on their values? JavaScript:根据 object 属性中的空间拆分数组中的对象 - JavaScript: Split objects in array based on space in object property 根据对象的日期年份将对象数组拆分为新数组 - Split array of objects into new arrays based on year of object's date 根据 object 中的值将对象数组拆分为两个 - Split an array of objects in to two, based on a value within the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM