这种排序阵列跳过一些元素是什么? 我有多种对象。我想排序它在同一位置中留下一些元素(b =“ not”) var a = [{a:1,b:“ yes”},{a:2,b:“ yes”},{a:5,b:“ not”},{a:0,b:“ not”},{a:0,b:“ yes”,y y y y y y y y y y y y y y y y y y y y y y y y y y y y yes”}] 功能...

问题描述 投票:0回答:3

var a=[{a:1,b:"YES"},{a:2,b:"YES"},{a:5,b:"NOT"},{a:0,b:"NOT"},{a:0,b:"YES"}] function sortc(x,y){ if (x.b=="NOT" || y.b=="NOT") return Infinity ; return (Number(x.a)-Number(y.a)) } console.log(a.sort(sortc));

结果是:
0: {a: 1, b: "YES"} 1: {a: 2, b: "YES"} 2: {a: 5, b: "NOT"} 3: {a: 0, b: "NOT"} 4: {a: 0, b: "YES"}

预期结果是(带有b =“ yes”的排序组件。):

{ "a": 0, "b": "YES" } { "a": 1, "b": "YES" } { "a": 5, "b": "NOT" } { "a": 0, "b": "NOT" } { "a": 2, "b": "YES" }

	

您不仅可以使用

Array#sort()
javascript arrays sorting object
3个回答
4
投票
您可以做什么,因为可以做什么

提取所有要分类的所有项目。 在原始数组上go,只替换应分类的任何东西,将其余的物品放在其位置。

var a = [ { a: 1, b: "YES" }, { a: 2, b: "YES" }, { a: 5, b: "NOT" }, { a: 0, b: "NOT" }, { a: 0, b: "YES" } ] //get only `b: "YES"` items const dataToSort = a.filter(item => item.b === "YES"); //sort them dataToSort.sort((x, y) => x.a - y.a); //replace only items that need to be sorted const it = dataToSort.values() for (let i = 0; i < a.length; i++) { if (a[i].b === "NOT") continue; a[i] = it.next().value; } console.log(a);
  1. 对于记录,最终的循环可以用更多的迭代用法替换,尽管它可能更令人困惑:
  2. const it = dataToSort.values() for (const [key, item] of a.entries()) { //use the key-value iterator from the array if (item.b === "NOT") continue; [a[key]] = it; //array destructuring internally advances an iterator }

var a = [ { a: 1, b: "YES" }, { a: 2, b: "YES" }, { a: 5, b: "NOT" }, { a: 0, b: "NOT" }, { a: 0, b: "YES" } ] //get only `b: "YES"` items const dataToSort = a.filter(item => item.b === "YES"); //sort them dataToSort.sort((x, y) => x.a - y.a); //replace only items that need to be sorted const it = dataToSort.values() for (const [key, item] of a.entries()) { if (item.b === "NOT") continue; [a[key]] = it; } console.log(a);

最终,可以通过辅助器功能

和少数小实用程序函数更方便地使其更方便

/* library code */ const transformArg = transform => f => (...args) => f(transform(...args)); function* filter(predicate, it) { for (const item of it) { if (predicate(item)) yield item; } } /* /library code */ var a = [ { a: 1, b: "YES" }, { a: 2, b: "YES" }, { a: 5, b: "NOT" }, { a: 0, b: "NOT" }, { a: 0, b: "YES" } ] /* helpers */ //extract the `b` key in this case so we don't need to repeat it. const getSortableAttribute = transformArg(({b}) => b); //get the value from key-value pair const getValue = transformArg(([, value]) => value); //check if the attribute is "YES" const isSortable = getSortableAttribute(attr => attr === "YES"); const dataToSort = a.filter(isSortable); dataToSort.sort((x, y) => x.a - y.a); const it = dataToSort.values() //iterate only over sortable key-value pairs by re-using the `isSortable` filter for (const [key, item] of filter(getValue(isSortable), a.entries())) { [a[key]] = it; } console.log(a);


这是一种直接使用

sort的方法,但使用

Proxy

length
和索引来构成访问。


0
投票

.as-console-wrapper { max-height: 100% !important; top: 0; }




    
@@@ninascholz答案

的某些变体,但没有可排序的零件。

输入参数只是确定可排序元素

的谓词

const
  sortOnly = (array, isSortable) => {
    const nonSortableIndicies = new Set();
    let shift = 0;
    var proxyArray = new Proxy(array, {
      get(target, prop) {
        if (!isFinite(prop)) return target[prop];
        if (isSortable(target[prop])) {
          return target[prop];
        } else
          nonSortableIndicies.add(+prop);
        return undefined;
      },
      set(target, prop, value) {
        if (!isFinite(prop)) {
          target[prop] = value;
          return true;
        }

        let index = +prop + shift;
        if (index >= target.length) return true;
        while (nonSortableIndicies.has(index)) {
          index++;
        }

        shift = index - prop;
        target[index] = value;
        return true;
      }
    });
    return proxyArray;
  },
  array = [{
    a: 1,
    b: "YES"
  }, {
    a: 2,
    b: "YES"
  }, {
    a: 5,
    b: "NOT"
  }, {
    a: 0,
    b: "NOT"
  }, {
    a: 0,
    b: "YES"
  }];

sortOnly(array, (val) => val.b !== 'NOT')
  .sort((a, b) => a.a - b.a)

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }


0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.