Javascript 如何获取现有数组中特定对象的计数数组?
我有这样一个对象数组:Javascript 如何获取现有数组中特定对象的计数数组?,javascript,arrays,Javascript,Arrays,我有这样一个对象数组: [ { importantKey: 'x', foo: 'bar', ... }, { importantKey: 'y', foo: 'bar', ... }, { importantKey: 'z', foo: 'bar', ... }, { importantKey: 'x', foo: 'bar', ... }, { impo
[
{
importantKey: 'x',
foo: 'bar',
...
},
{
importantKey: 'y',
foo: 'bar',
...
},
{
importantKey: 'z',
foo: 'bar',
...
},
{
importantKey: 'x',
foo: 'bar',
...
},
{
importantKey: 'y',
foo: 'bar',
...
},
{
importantKey: 'z',
foo: 'bar',
...
},
...
]
keysArray.map(key => values.filter(v => v.importantKey === key).length);
另一个数组的值为importantKey
:
keysArray = [x, y, z]
如何获得一个数组,该数组的值是所有对象的计数,这些对象的每个importantKey
的顺序与keysArray
的顺序相同?因此,最终结果将是:
[ numberOfObjectsWithKeyX, numberOfObjectsWithKeyY, numberOfObjectsWithKeyZ ]
[2, 2, 2]
对于本例,结果将是:
[ numberOfObjectsWithKeyX, numberOfObjectsWithKeyY, numberOfObjectsWithKeyZ ]
[2, 2, 2]
另外,
keysArray
是动态生成的,因此,x
、y
和z
不能硬编码。您可以这样做:
[
{
importantKey: 'x',
foo: 'bar',
...
},
{
importantKey: 'y',
foo: 'bar',
...
},
{
importantKey: 'z',
foo: 'bar',
...
},
{
importantKey: 'x',
foo: 'bar',
...
},
{
importantKey: 'y',
foo: 'bar',
...
},
{
importantKey: 'z',
foo: 'bar',
...
},
...
]
keysArray.map(key => values.filter(v => v.importantKey === key).length);
基本上,您可以在keysArray
上调用map()
,这将创建一个与之并行的数组,其中包含map()
输出的任何值
对于该值,只需调用values.filter()
并仅过滤出具有适当键的内容,然后只需检查长度即可获得计数
const值=[
{
importantKey:'x',
福:“酒吧”
},
{
重要键:“y”,
福:“酒吧”
},
{
重要键:“y”,
福:“酒吧”
},
{
importantKey:'x',
福:“酒吧”
},
{
重要键:“y”,
福:“酒吧”
},
{
重要键:“z”,
福:“酒吧”
}
];
常量键=['x','y','z'];
const result=keys.map(key=>values.filter(v=>v.importantKey===key.length);
控制台日志(结果)使用数组映射
函数,然后使用过滤器
方法进行编码
map
函数将数组转换为另一个数组。
filter
函数允许使用指定的谓词筛选数组。在这种情况下,需要比较importantKey
值
var arr1=[
{
importantKey:'x',
福:“酒吧”,
},
{
重要键:“y”,
福:“酒吧”,
},
{
重要键:“z”,
福:“酒吧”,
},
{
importantKey:'x',
福:“酒吧”,
},
{
重要键:“y”,
福:“酒吧”,
},
{
重要键:“z”,
福:“酒吧”,
},
{
importantKey:'x',
福:“酒吧”,
}
];
var keysArray=['x','y','z']
log(keysArray.map(a=>arr1.filter(b=>b.importantKey==a.length))代码>可以使用哈希表作为结果数组索引的引用
该方案使用slingle循环来迭代数据
var data=[{importantKey:'x',foo:'bar'},{importantKey:'y',foo:'bar'},{importantKey:'z',foo:'bar'},{importantKey:'x',foo:'bar'},{importantKey:'y',foo:'bar'},{importantKey:'z',foo:'bar'},
keysArray=['x','y','z'],
hash=Object.create(null),
result=keysArray.map(函数(k,i){hash[k]=i;返回0;});
data.forEach(函数(o){
结果[散列[o.importantKey]]++;
});
控制台日志(结果)代码>简单易懂的解决方案
读取数据
插入到地图中
遍历map
按[x,y,z]
var数据=
[
{importantKey:'z',foo:'bar'},
{importantKey'y',foo'bar'},
{importantKey:'z',foo:'bar'},
{importantKey:'x',foo:'bar'},
{importantKey'y',foo'bar'},
{importantKey:'z',foo:'bar'},
]
document.write(JSON.stringify(data)+“
”;
常量映射={};
对于(数据中的i)
{
const key=data[i]。importantKey;
地图[键]=(地图[键]| | 0)+1;
document.write(key);//zyzyz
}
document.write(“
map=“+JSON.stringify(map));//{“z”:3,“y”:2,“x”:1}
常数k=[];
(我在地图上){
//k、 推(map[i]);
k、 拼接(映射[i]-'x',0,映射[i]);
}
document.write(“
k=“+JSON.stringify(k));//[1,2,3]
请添加示例所需的结果。结果可能是对象而不是数组吗?@Matt,请查看我使用映射的简易解决方案
谢谢。不幸的是,这正是我已经拥有的实现,我得到了正确数量的元素和数组,但它们都是未定义的
,而不是数字。问题一定与以下事实有关:键
和值
都是由rxjs可观察对象动态创建的。无论如何,谢谢你。至少我知道这不是问题所在。