2016-07-27 103 views
0

我有下面这个对象数组。循环遍历一组对象并获得只有几个对象

[Object, Object, Object] 
    0:Object 
     name: "Rick" 
     Contact: "Yes" 
     id: 1 
    1:Object 
     name:"Anjie" 
     Contact:"No" 
     id: 2 
    2:Object 
     name:"dillan" 
     Contact:"Maybe" 
     id:3 

现在,我只想要它的名称和联系人对象。我怎么能得到这个。另外,在其他情况下,我想要名称和ID。请有人让我知道如何实现这一点。

例如,只有名字和联系人应该给出这个结果。

[Object, Object, Object] 
    0:Object 
     name: "Rick" 
     Contact: "Yes" 
    1:Object 
     name:"Anjie" 
     Contact:"No" 
    2:Object 
     name:"dillan" 
     Contact:"Maybe" 
+1

什么你不知道该怎么办?看起来像一个非常简单的问题。你卡在哪里? – 2016-07-27 19:38:46

+1

[此前一个问题](http://stackoverflow.com/questions/38248004/convert-array-of-objects-and-their-properties-to-array)你的很多东西都可以在这里找到。 – 2016-07-27 19:40:00

+0

name和contact是Object的属性。它们是弦乐,当然它们是物体。但将它们描述为属性会更清楚。无论如何,你可以使用Array.map来实现这一点 –

回答

1

最简单的方法是使用map功能:

var objs = objs.map(function(obj) { 
    return { 
     name: obj.name, 
     Contact: obj.Contact 
    } 
}); 

或者,您可以遍历它手动:

var objs = [{ 
 
     name: "Rick", 
 
     Contact: "Yes", 
 
     id: 1 
 
}, { 
 
     name:"Anjie", 
 
     Contact:"No", 
 
     id: 2 
 
}, { 
 
     name:"dillan", 
 
     Contact:"Maybe", 
 
     id:3 
 
}]; 
 

 
var newObjs = []; 
 

 
for (var i=0, len = objs.length; i < len; i++) 
 
{ 
 
    newObjs.push({ 
 
     name: objs[i].name, 
 
     Contact: objs[i].Contact 
 
    }); 
 
} 
 

 
console.log(newObjs);

1

如果你想访问数组中对象的一些属性不是简单的,你可以使用一个for循环和传递价值的我喜欢: -

for(var i=0;i<object.length;i++){ 
    object[i].name // give you name 
    object[i].contact // give you contact do whatever you want to do with this 
} 

或者干脆写一个函数,使一个单独的对象从数组的对象。

1

你可以使用下划线的mappick

var result = _.map(data, item => _.pick(item, 'name', 'Contact')); 
0

你可以使用map()和返回与已删除属性的新对象的数组。要创建对象的副本,你可以使用(JSON.parse(JSON.stringify()))

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }]; 
 

 
var result = data.map(function(e) { 
 
    var r = (JSON.parse(JSON.stringify(e))); 
 
    delete r.id; 
 
    return r; 
 
}); 
 

 
console.log(result) 
 
console.log(data)

或者你的使用Object.assign({}, e)

var data = [{ id: 1, name: "Rick", Contact: "Yes" }, { id: 2, name: "Anjie", Contact: "No" }, { id: 3, name: "dillan", Contact: 'Maybe' }]; 
 

 
var result = data.map(function(e) { 
 
    var r = Object.assign({}, e); 
 
    delete r.id; 
 
    return r; 
 
}); 
 

 
console.log(result); 
 
console.log(data);

1

一个通用的解决方案coould是一个给定的数据数组和键阵列。所需的属性映射到一个新的数组中。

function getWithKeys(array, keys) { 
 
    return array.map(function (a) { 
 
     var o = {}; 
 
     keys.forEach(function (k) { 
 
      o[k] = a[k]; 
 
     }); 
 
     return o; 
 
    }); 
 
} 
 

 
var array1 = [{ name: "Rick", Contact: "Yes", id: 1 }, { name: "Anjie", Contact: "No", id: 2 }, { name: "dillan", Contact: "Maybe", id: 3 }], 
 
    array2 = getWithKeys(array1, ['name', 'Contact']), 
 
    array3 = getWithKeys(array1, ['name', 'id']); 
 

 
console.log(array2); 
 
console.log(array3);