[英]How can I filter only a particular data from an array of objects
If its an object, you can just access its properties by calling its name after the dot (eg myObject.mobile
) or by bracket syntax: myObject['mobile']
如果它是一个对象,您可以通过在点之后调用其名称(例如myObject.mobile
)或通过括号语法来访问其属性: myObject['mobile']
All you need to know: https://www.w3schools.com/js/js_properties.asp所有你需要知道的: https : //www.w3schools.com/js/js_properties.asp
If it's a collection, just use forEach
or map
function (or any other available method) to iterate over it and then access property as shown above.如果它是一个集合,只需使用forEach
或map
函数(或任何其他可用方法)对其进行迭代,然后访问如上所示的属性。
You can use the library LoDash .您可以使用库LoDash 。 Add the library to your project and use .pick method.将库添加到您的项目并使用 .pick 方法。 If your data is an Array, then use .map method如果您的数据是数组,则使用 .map 方法
var data = {
var1: 1,
var2: 2,
var3: 3
};
console.log(data);
var mappedData = _.pick(data, 'var2' );
console.log(mappedData);
If your data is an object,如果您的数据是一个对象,
var data = { entryId: 'ABC', mobile: '0123456789' }
console.log(data.entryId) // 'ABC'
console.log(data.mobile) // '0123456789'
If your data is an array of objects,如果您的数据是一个对象数组,
var data = [
{ entryId: 'ABC', mobile: '0123456789' },
{ entryId: 'BBB', mobile: '1111111111' },
{ entryId: 'CCC', mobile: '2222222222' }
];
var dataWithMobileNumbersOnly = data.map((obj) => {
return { mobile: obj.mobile };
});
console.log(dataWithMobileNumbersOnly) // [{mobile: '0123456789'}, {mobile: '1111111111'}, {mobile: '2222222222'}]
For more information about map() method, visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map有关 map() 方法的更多信息,请访问https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.