[英]How to filter the object using lodash or javascript
我正在从服务器获取这样的对象,
var data = {
test1: {
documents: []
},
test2: {
documents: [{
vId: 'sdfas23',
TypeId: '81',
isDeleted: false
}],
answer: true
},
test3: {
documents: [{
vId: 'H1mJinyI',
TypeId: '82',
isDeleted: false
}],
answer: true
}
}
我想从对象中过滤typeId,我期望的结果是
[81,82]
有人可以帮忙吗
如果您的documents
只有一个或零个对象(例如您的示例),则可以将reduce()
与Object.keys()
var data = {"test1":{"documents":[]},"test2":{"documents":[{"vId":"sdfas23","TypeId":"81","isDeleted":false}],"answer":true},"test3":{"documents":[{"vId":"H1mJinyI","TypeId":"82","isDeleted":false}],"answer":true}} var result = Object.keys(data).reduce(function(a, e) { if (data[e].documents[0]) a.push(data[e].documents[0].TypeId); return a; }, []) console.log(result)
对于documents
数组中的多个对象,您可以在前面的代码中添加forEach
循环,以便您可以做到这一点( test3.documents
有两个对象)
var data = {"test1":{"documents":[]},"test2":{"documents":[{"vId":"sdfas23","TypeId":"81","isDeleted":false}],"answer":true},"test3":{"documents":[{"vId":"H1mJinyI","TypeId":"82","isDeleted":false},{"vId":"Random","TypeId":"100","isDeleted":false}],"answer":true}} var result = Object.keys(data).reduce(function(a, e) { if (data[e].documents.length) data[e].documents.forEach(function(p) { a.push(p.TypeId); }); return a; }, []) console.log(result);
您可以只用一行代码来完成此操作:
_.map(_.flatten(_.map(_.values(data), 'documents')), 'TypeId');
这将产生:
["81", "82"]
您可以执行以下操作:
data[key]
来获取对象。 obj.documents
。 现在文档是一个数组,我们需要在其中的每个对象中获取TypeId
。 为此,您可以拥有一个数组并在其中手动推送值,也可以使用Array.map 。 .map
,它将为每个对象返回一个数组。 因此,您将必须使用array.concat
将其附加到相同的函数。 但是,如果使用.push
,则跳过此步骤。 var data={test1:{documents:[]},test2:{documents:[{vId:"sdfas23",TypeId:"81",isDeleted:!1}],answer:!0},test3:{documents:[{vId:"H1mJinyI",TypeId:"82",isDeleted:!1}],answer:!0}}; var r = []; for(var k in data){ r = r.concat(data[k].documents.map(function(o){return o.TypeId})); } console.log(r)
这给了您预期的结果,尽管不确定您是否会满意
var data= {
test1: { documents: [] },
test2: { documents: [{ vId: 'sdfas23',
TypeId: '81',
isDeleted: false }], answer: true },
test3:
{ documents:
[{ vId: 'H1mJinyI',
TypeId: '82',
isDeleted: false }],
answer: true }
}
function getIds(array) {
var ret = [];
for (key in data) {
for (key2 in data[key].documents) {
ret.push(data[key].documents[key2].TypeId)
}
}
return ret
}
console.log(getIds(data));
尝试这个
Object.getOwnPropertyNames(data).map(function(val, key) {
return data[val].documents[0] ? data[val].documents[0].TypeId : '';
})
如果属性“ documents”始终只有一个对象,则只需使用_.compact(_.map(data, 'documents[0].TypeId'));
如果属性“ documents”有很多对象,请使用_.flatten(_.map(data, (item) => _.map(item.documents, 'TypeId')));
这是先前答案的另一个简化版本(没有reduce和fancy的东西仅映射):
var data= { test1: { documents: [] },
test2: { documents: [{ vId: 'sdfas23', TypeId: '81', isDeleted: false },{ vId: 'sdfas23', TypeId: '85', isDeleted: false }], answer: true },
test3: { documents: [{ vId: 'H1mJinyI', TypeId: '82', isDeleted: false }], answer: true }
};
var getTypeIds = function(data){
var ids = [];
// get object keys in this case : test1,test2,test3
Object.keys(data).map(function(key){
// loop through each document attribute and extract TypeId attributes
data[key].documents.map(function(item){
ids.push(item.TypeId);
});
})
return ids;
}
console.log(getTypeIds(data)); // [ '81', '85', '82' ]
您可以使用lodash的_.map函数获取所需的结果。 这是示例代码-
var result = _.map(data, function(v,i){
if(_.isArray(v.documents)) {
return v.documents[0].TypeId
}
return ''
}
在lodash中解决此问题的最简单方法是使用flatMap()获取平整的documents
数组,并使用map()获得所有TypeId
。
var result = _(data)
.flatMap('documents')
.map('TypeId')
.value();
var data = { test1: { documents: [] }, test2: { documents: [{ vId: 'sdfas23', TypeId: '81', isDeleted: false }], answer: true }, test3: { documents: [{ vId: 'H1mJinyI', TypeId: '82', isDeleted: false }], answer: true } }; var result = _(data) .flatMap('documents') .map('TypeId') .value(); console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>
使用ES6的普通JS解决方案应该是这样的:
var result = Object.keys(data) // get keys as reference when mapping
.map(v => data[v].documents || []) // get all documents
.reduce((x, y) => x.concat(y)) // flatten
.map(v => v.TypeId); // get TypeId
var data = { test1: { documents: [] }, test2: { documents: [{ vId: 'sdfas23', TypeId: '81', isDeleted: false }], answer: true }, test3: { documents: [{ vId: 'H1mJinyI', TypeId: '82', isDeleted: false }], answer: true } }; var result = Object.keys(data) .map(v => data[v].documents || []) .reduce((x, y) => x.concat(y)) .map(v => v.TypeId); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.