【问题标题】:find different matches and resort array in javascript在 javascript 中查找不同的匹配项和度假村数组
【发布时间】:2017-04-20 22:03:35
【问题描述】:

我想知道如何根据每个对象中是否存在属性来使用数组。这是从后端返回的内容,只有一个具有 something 字段,并且只有一个具有其 isActive 属性的 false 值。

我需要一个或多个具有 some 属性排序到数组顶部的一个或一个,其次,所有 isActive 属性值为 true 的都排在具有 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 来完成?

【问题讨论】:

  • "something" 道具周围的括号使其无效

标签: javascript jquery arrays sorting


【解决方案1】:

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

编辑:

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

【讨论】:

  • 感谢您对它的精彩解释。还有一件事 var arr = [{"_id":"58f8ffe7ce2bbf01c6164801","isActive":true,"Something": [{ "xyz": "xyz" }]}];如何使用 hasOwnProperty 来简单地检查 Something 内部是否有任何对象?
  • @totalnoob 你的意思是检查something里面有没有东西?
  • 是的,在您的插件中。如果不是检查“某物”是否存在,而是检查“某物”内部是否有任何对象。它会更像 if((a.something.length > 0) 吗?
  • @totalnoob 如果something 不包含任何内容,则只需使用其isActive 对其进行排序,无论它在结果数组中的哪个位置。对吗?
  • 检查上面的 EDIT: 部分!
【解决方案2】:

这是我在 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 } ]

【讨论】:

  • 您能否提供一些详细信息来说明它的作用,因为它是如此简化?
  • 我更新了我的答案。除了箭头函数之外,没有太多的 es6 发生。为了简洁起见,我基本上使用Conditional (ternary) Operator。三元运算符也可以嵌套,我就是这样做的。首先检查something 属性是否存在,然后检查isActive 是否为真。
猜你喜欢
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 2012-06-20
  • 1970-01-01
相关资源
最近更新 更多