在javascript中更改对象数组顺序的最佳方法是什么?

时间:2016-11-17 23:52:20

标签: javascript arrays

如下所示重新排序对象数组的最简单方法是什么。

我想根据组值(自定义顺序)重新排序

[ { groupValue: 'Corp',
    doclist: { numFound: 259, start: 0, maxScore: 1.1320078, docs: [Object] } },
  { groupValue: 'Phone',
    doclist: { numFound: 7975, start: 0, maxScore: 1.2560269, docs: [Object] } },
  { groupValue: 'Shop',
    doclist: { numFound: 9, start: 0, maxScore: 1.2556685, docs: [Object] } } ]

如果我使用组值传递参数,那么对应于组值的特定对象应该出现在顶部

这样的东西
function ("Shop",originalArray){
 return newArray;
}

应该返回:

    [ { groupValue: 'Shop',
        doclist: { numFound: 9, start: 0, maxScore: 1.2556685, docs: [Object] } },
{ groupValue: 'Corp',
        doclist: { numFound: 259, start: 0, maxScore: 1.1320078, docs: [Object] } },
      { groupValue: 'Phone',
        doclist: { numFound: 7975, start: 0, maxScore: 1.2560269, docs: [Object] } }]

4 个答案:

答案 0 :(得分:4)

这应该有用,给出一个新的数组,并且应该只通过一次。



var data = [{
  groupValue: 'Corp',
  doclist: {
    numFound: 259,
    start: 0,
    maxScore: 1.1320078,
    docs: [Object]
  }
}, {
  groupValue: 'Phone',
  doclist: {
    numFound: 7975,
    start: 0,
    maxScore: 1.2560269,
    docs: [Object]
  }
}, {
  groupValue: 'Shop',
  doclist: {
    numFound: 9,
    start: 0,
    maxScore: 1.2556685,
    docs: [Object]
  }
}];

function goFirst(d, daFirst) {
  var r = [];

  d.forEach((e) => {
    if (e['groupValue'] === daFirst)
      r.unshift(e);
    else
      r.push(e);
  })

  return r;
}

console.log(goFirst(data, 'Shop'));




答案 1 :(得分:1)

您可以将自定义功能传递给Array.prototype.sort

function sortBy(arr, val, prop) {
    return arr.sort(function(a,b) {
        if (b[prop] == val) return 1;
        return 0;
    });
 }

console.log(sortBy(arr, "Shop", "groupValue"))

由于其余元素的顺序对你很重要(应该保持不变),请使用:

function sortBy(arr, val, prop) {
  var top = [];
  var rest = [];
    /* see https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for...of 
    for "for of" loops, you might want to use a forEach() loop as "for of" isn't supported in many browsers as of now
    */
  for (var el of arr) {  
    if (el[prop] == val) {
        top.push(el)
    } else {
        rest.push(el);
    }
  }
  return top.concat(rest);
}

 console.log(sortBy(arr, "Shop", "groupValue"))

答案 2 :(得分:1)

您可以使用 splice 移动数组中的元素:









  var arr = [{groupValue:'Corp',
 doclist:{numFound:259,start:0,maxScore:1.1320078}},
 {groupValue:'电话',
 doclist:{numFound:7975,start:0,maxScore:1.2560269}},
 {groupValue:'Shop',
 doclist:{numFound:9,start:0,maxScore:1.2556685}}];

 function move(option,originalArray){
 var newArray = originalArray.slice(); &#的xD;
 newArray.splice(0,0,newArray.filter(item => item.groupValue == option)[0]);
 return newArray;
}

 console.log(move(“Shop”,arr));  
&#的xD;

&#的xD;

&#的xD;






<代码> splice 首先0表示放置新值的位置。第二个0表示如何处理其他元素(0表示移动它们),第三个参数是值。

&#xA;

答案 3 :(得分:0)

可以使用“排序”功能,根据值先放置item

var originalArray = [{
        groupValue: 'Corp',
        doclist: {
            numFound: 259,
            start: 0,
            maxScore: 1.1320078,
            docs: [Object]
        }
    },
    {
        groupValue: 'Phone',
        doclist: {
            numFound: 7975,
            start: 0,
            maxScore: 1.2560269,
            docs: [Object]
        }
    },
    {
        groupValue: 'Shop',
        doclist: {
            numFound: 9,
            start: 0,
            maxScore: 1.2556685,
            docs: [Object]
        }
    }
]

var reorder = function (attr, originalArray){
    var newArray = originalArray.slice() // clone array
    newArray.sort(function(a){return a.groupValue !== attr?1:-1}) // put in first
    return newArray;
}

// groupValue === Shop in first
console.log(reorder("Shop", originalArray))