我正在尝试过滤数组对象的数组对象内的数据,请查找以下代码以获取更多信息。
var data = [
{
name:'testdata1',
subdata:[{status:'fail'},{status:'success'}]
},
{
name:'testdata2',
subdata:[{status:'fail'},{status:'success'}]
}
]
预期数据:
var successdata = [
{
name:'testdata1',
subdata:[status:'success'}]
},
{
name:'testdata2',
subdata:[status:'success'}]
}
];
var FailureData =[
{
name:'testdata1',
subdata:[{status:'fail'}]
},
{
name:'testdata2',
subdata:[{status:'fail'}]
}
];
我错过了花括号,所以我要更新
答案 0 :(得分:1)
您可以使用Array.map()映射数组:
var successData = data.map(item => ({name: item.name, subdata:[{status:'success'}]})
答案 1 :(得分:1)
var data = [{name:'testdata1',subdata:[{status:'fail'}, {status:'success'}] },{name:'testdata2',subdata:[{status:'success'}, {status:'fail'}] }]
var successData = filterByStatus('success', data);
var failureData = filterByStatus('fail', data);
function filterByStatus(status, data) {
return data.map(d => {
var newObj = Object.assign({}, d);
newObj.subdata = newObj.subdata.filter(s => s.status === status);
return newObj;
});
}
console.log('successData', successData);
console.log('failureData', failureData);
如果对象中具有一个成功属性,则可以执行您想要的操作之一
答案 2 :(得分:1)
我想您要做的是根据子数据状态过滤数组。 我还猜想子数据应该只有status属性,而您的代码应该是:var data = [{name:'testdata1',subdata:[{status:'fail'},{status:'success'}]}
然后,您要查看子数据数组,并找出其中包含成功和失败的数据。
所以您可能要寻找的是这个
datastore.runQuery(query, (err,entities) => {
var searches = [];
if(typeof entities !== 'undefined' && entities.length > 0) {
var keys = entities.map(function(entity) {
searches.push(entity);
});
//Setting the response object
response.status="OK";
response.message="";
response.data={
"searches": searches
};
}else{
response.status="OK";
response.message="Empty result";
}
//Returning the response to client
res.status(200).send(JSON.stringify(response));
});
与failureData相同。
有关更多信息,您可以检查Array.prototype.filter函数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
P.S。如您对问题的评论中所述,子数据数组不能是具有两个相同属性的对象
答案 3 :(得分:1)
希望这会有所帮助。
SELECT ISNULL(Column_to_check,'') FROM SomeTable