我想知道如何根据每个对象中是否存在属性来使用数组。这是从后端返回的内容,只有一个具有something字段,并且只有一个字段的isActive属性具有false值。
我需要一个或多个具有某个属性排序到数组顶部的属性,其次所有具有真实值的isActive属性都位于带有false的属性之前。
所以这个
var obj = [
{
"_id": "58f8ffe7ce2bbf01c6164802",
"isActive": false
},
{
"_id": "58f8ffe7ce2bbf01c6164803",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164804",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164801",
"isActive": true,
{ "something": "xyz" }
},
{
"_id": "58f8ffe7ce2bbf01c6164806",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164807",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164808",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164809",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164810",
"isActive": true
}
]
会变成
var obj = [
{
"_id": "58f8ffe7ce2bbf01c6164801",
"isActive": true,
{ "something": "xyz" }
},
{
"_id": "58f8ffe7ce2bbf01c6164803",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164804",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164806",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164807",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164808",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164809",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164810",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164802",
"isActive": false
},
]
两种不同类型的搜索和匹配,然后是array.push?像lodash这样的助手是否实用,使用jQuery会更容易,还是可以简单地用vanilla js完成?
答案 0 :(得分:3)
m
修改强>
var arr = [{"_id":"58f8ffe7ce2bbf01c6164802","isActive":false,"something":"xyz"},{"_id":"58f8ffe7ce2bbf01c6164803","isActive":false},{"_id":"58f8ffe7ce2bbf01c6164804","isActive":true},{"_id":"58f8ffe7ce2bbf01c6164801","isActive":true,"something":"xyz"},{"_id":"58f8ffe7ce2bbf01c6164806","isActive":true},{"_id":"58f8ffe7ce2bbf01c6164807","isActive":true},{"_id":"58f8ffe7ce2bbf01c6164808","isActive":true},{"_id":"58f8ffe7ce2bbf01c6164809","isActive":true},{"_id":"58f8ffe7ce2bbf01c6164810","isActive":true}];
arr.sort(function(a, b) {
if(a.hasOwnProperty("something") && !b.hasOwnProperty("something")) // if a has the property "something" and b doesn't
return -1; // then put a above b
else if(!a.hasOwnProperty("something") && b.hasOwnProperty("something")) // otherwise if b has the property and a doesn't
return 1; // then put b above a
// otherwise, either both or none of them have it (then sort using the property "isActive")
return a.isActive? -1: 1; // if a has "isActive" set to true put it on top of b without checking for b's "isActive", otherwise, put it below
});
console.log(arr);
答案 1 :(得分:1)
以下是我在ES6中的表现。
下面的排序逻辑只测试属性something
的存在,如果不存在,则检查属性isActive
let obj = [
{
"_id": "58f8ffe7ce2bbf01c6164803",
"isActive": false
},
{
"_id": "58f8ffe7ce2bbf01c6164804",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164803",
"isActive": false
},
{
"_id": "58f8ffe7ce2bbf01c6164804",
"isActive": true
},
{
"_id": "58f8ffe7ce2bbf01c6164801",
"isActive": true,
"something": "xyz"
}
];
obj.sort((a, b) => (
(a.hasOwnProperty('something') || b.hasOwnProperty('something'))
? 1 // if the above line evaluates to true, return one
: (a.isActive ? -1 : 1) // else evaluate this
));
console.log(obj);
// [ { _id: '58f8ffe7ce2bbf01c6164801',
// isActive: true,
// something: 'xyz' },
// { _id: '58f8ffe7ce2bbf01c6164804', isActive: true },
// { _id: '58f8ffe7ce2bbf01c6164804', isActive: true },
// { _id: '58f8ffe7ce2bbf01c6164803', isActive: false },
// { _id: '58f8ffe7ce2bbf01c6164803', isActive: false } ]