Javascript 如何操作数组对象并创建新数组
我有一个js数组,如下所示:Javascript 如何操作数组对象并创建新数组,javascript,arrays,Javascript,Arrays,我有一个js数组,如下所示: var names = [{name:"high",id:1},{name:"high",id:2}, {name:"low",id:1}, {name:"low",id:2},{name:"medium",id:1},{name:"medium",id:2}]; 我需要用这样的方法创建另一个数组 var newArr=[{name:high,items:[1,2]},{name:low,items:[1,2]},{name:medium,items:[1,2
var names = [{name:"high",id:1},{name:"high",id:2},
{name:"low",id:1}, {name:"low",id:2},{name:"medium",id:1},{name:"medium",id:2}];
我需要用这样的方法创建另一个数组
var newArr=[{name:high,items:[1,2]},{name:low,items:[1,2]},{name:medium,items:[1,2]}];
请建议我如何做。基于
纯js
var obj={};
对于(var i=0;i
基于Web的解决方案
纯js
var obj={};
对于(var i=0;i
这是一个简单易懂的解决方案,无需外部库
var newArr = []
for ( var i in names ) {
var exists = findItemInArray(names[i].name,newArr);
if(exists) { // Another item with same name was added to newArray
var items = exists.items.push(names[i].id); // We add the id to existing item
} else { // No other item with same name was added to newArray
var newItem = {name:names[i].name, items:names[i].id};
newArr.push(newItem);
}
}
我使用这个函数来返回newArray中已经存在的项
function findItemInArray(name,array){
for(var i in array){
if(array[i].name === name){
return array[i];
}
return null;
}
这是一个简单易懂的解决方案,无需外部库
var newArr = []
for ( var i in names ) {
var exists = findItemInArray(names[i].name,newArr);
if(exists) { // Another item with same name was added to newArray
var items = exists.items.push(names[i].id); // We add the id to existing item
} else { // No other item with same name was added to newArray
var newItem = {name:names[i].name, items:names[i].id};
newArr.push(newItem);
}
}
我使用这个函数来返回newArray中已经存在的项
function findItemInArray(name,array){
for(var i in array){
if(array[i].name === name){
return array[i];
}
return null;
}
您正在寻找一个分组函数。尝试:
您正在寻找一个分组函数。尝试:
以下是一个不使用外部库的解决方案:
/**
* Add the given object to the given set.
*
* @param {object} theSet The set to add this object to.
* @param {object} theObject The object to add to the set.
*
* @return {object} theSet, with theObject added to it.
*
* @note Assumes that theObject.name should be the key,
* while theObject.id should go into the value array.
* @note This is an Array.prototype.reduce() callback.
*/
function collect(theSet, theObject) {
if (theSet.hasOwnProperty(theObject.name)) {
theSet[theObject.name].push(theObject.id);
} else {
theSet[theObject.name] = [theObject.id];
}
return theSet;
}
var names = [{name:"high",id:5},{name:"high",id:6},
{name:"low",id:1}, {name:"low",id:2},{name:"medium",id:3},{name:"medium",id:4}],
combinedSet = names.reduce(collect, {}), // This is Step 1
final = [],
key;
// This is Step 2
for (key in combinedSet) {
if (combinedSet.hasOwnProperty(key)) {
final.push(
{
"name" : key,
"items": combinedSet[key]
}
);
}
}
第一步是将ID分组到对象名称下。我使用Array.prototype.reduce
来实现这一点,使用回调collect
。该转换的结果进入combinedSet
变量
第二步是将我们在步骤1中创建的集合转换为最终数组:使用集合的键作为
名称
成员创建对象,并使用其值作为项
成员。我不能像以前那样使用reduce
,所以我使用了一个简单的for
循环。注意,我用一个hasOwnProperty
检查来包装东西,以防止有人修改了Object.prototype
;如果我不这样做,那么集合中可能会有更多我没有放在那里的项目,这将引入bug。下面是一个不使用外部库的解决方案:
/**
* Add the given object to the given set.
*
* @param {object} theSet The set to add this object to.
* @param {object} theObject The object to add to the set.
*
* @return {object} theSet, with theObject added to it.
*
* @note Assumes that theObject.name should be the key,
* while theObject.id should go into the value array.
* @note This is an Array.prototype.reduce() callback.
*/
function collect(theSet, theObject) {
if (theSet.hasOwnProperty(theObject.name)) {
theSet[theObject.name].push(theObject.id);
} else {
theSet[theObject.name] = [theObject.id];
}
return theSet;
}
var names = [{name:"high",id:5},{name:"high",id:6},
{name:"low",id:1}, {name:"low",id:2},{name:"medium",id:3},{name:"medium",id:4}],
combinedSet = names.reduce(collect, {}), // This is Step 1
final = [],
key;
// This is Step 2
for (key in combinedSet) {
if (combinedSet.hasOwnProperty(key)) {
final.push(
{
"name" : key,
"items": combinedSet[key]
}
);
}
}
第一步是将ID分组到对象名称下。我使用Array.prototype.reduce
来实现这一点,使用回调collect
。该转换的结果进入combinedSet
变量
第二步是将我们在步骤1中创建的集合转换为最终数组:使用集合的键作为
名称
成员创建对象,并使用其值作为项
成员。我不能像以前那样使用reduce
,所以我使用了一个简单的for
循环。注意,我用一个hasOwnProperty
检查来包装东西,以防止有人修改了Object.prototype
;如果我不这样做,那么可能会有更多的项目在集合中,我没有放在那里,这将引入错误。谢谢。。我已经使用了纯js的一个,它是完美的工作在我这边。谢谢,谢谢。。我已经使用了纯js的一个,它是完美的工作在我这边。谢谢