簡體   English   中英

在javascript中更改對象數組順序的最佳方法是什么

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

如下重新排序對象數組的最簡單方法是什么。

我想根據組值重新排序(自定義順序)

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

這應該可以工作,提供一個新數組並且應該只遍歷原始數組一次。

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

您可以將自定義函數傳遞給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"))

您可以使用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 表示放置新值的位置。 第二個 0 表示如何處理其他元素(0 表示移動它們),第三個參數是值。

您可以使用“排序”功能,根據值先放置項目

 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