简体   繁体   English

在JavaScript中找到不同的匹配项和度假胜地数组

[英]find different matches and resort array in javascript

I'm wondering how one would resort an array based on whether or not a property exists within each object. 我想知道如何根据每个对象中是否存在属性来求助于数组。 this is what would come back from the backend, only one has the something field, and only one has a false value for its isActive property. 这就是从后端返回的内容,只有一个具有something字段,并且只有一个具有false的isActive属性值。

I'd need the one or ones that has a something property sorted to the top of the array and secondly all the ones with a true value for its isActive property comes before the ones with false. 我需要具有在数组顶部排序的something属性的一个或多个,其次所有具有isActive属性值为true的属性都位于具有false的属性之前。

so this 所以这

  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 } ] 

would become 会成为

  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 }, ] 

two different types of searching and matching and then an array.push? 两种不同类型的搜索和匹配,然后是array.push? would helpers like lodash be practical, would it be easier with jQuery or can it simply be done with vanilla js? 像lodash这样的助手会实用吗?使用jQuery会更容易吗?还是可以使用vanilla js轻松完成?

 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); 

Edit: 编辑:

 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) { var aHasIt = a.something && a.something.length > 0, bHasIt = b.something && b.something.length > 0; if(aHasIt && !bHasIt) return -1; else if(!aHasIt && bHasIt) return 1; return a.isActive? -1: 1; }); console.log(arr); 

Here is how I would do it in ES6. 这是我在ES6中的做法。

The sort logic below simply tests the existence of the property something , if it doesn't, then checks the property isActive 下面的排序逻辑只是测试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 } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM