简体   繁体   English

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

[英]What is the best way to change the order of array of objects in javascript

What is the easiest way to re-order an array of objects as below.如下重新排序对象数组的最简单方法是什么。

I want to re-order it based on group value (custom order)我想根据组值重新排序(自定义顺序)

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

If I pass in argument with the group value, that particular object corresponding to the group value should come up on the top如果我传入带有组值的参数,则对应于组值的特定对象应该出现在顶部

Something like类似的东西

function ("Shop",originalArray){
 return newArray;
}

Should return:应该返回:

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

This should work, gives a new array and should go through the original just once.这应该可以工作,提供一个新数组并且应该只遍历原始数组一次。

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

You can pass a custom function to Array.prototype.sort :您可以将自定义函数传递给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"))

As the order for the rest of the elements is important for you (should stay the same), go with this:由于其余元素的顺序对您很重要(应该保持不变),请执行以下操作:

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

You can to use splice to move the elements in the array:您可以使用splice移动数组中的元素:

 var arr = [ { groupValue: 'Corp', doclist: { numFound: 259, start: 0, maxScore: 1.1320078 } }, { groupValue: 'Phone', 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(); newArray.splice(0,0,newArray.filter(item => item.groupValue == option)[0]); return newArray; } console.log(move("Shop",arr));

splice first 0 indicate where to put the new value. splice first 0 表示放置新值的位置。 The second 0 indicate what to do with the other elements (0 indicate move them), the third argument is the value.第二个 0 表示如何处理其他元素(0 表示移动它们),第三个参数是值。

You can use the "sort" function to place the item first according to the value您可以使用“排序”功能,根据值先放置项目

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

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

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