简体   繁体   English

如何根据另一个数组 JavaScript 更改对象数组中元素的索引?

[英]How to change index of elements in array of objects according to another array JavaScript?

I have two arrays and need to change the order of elements in a way to be similar.我有两个 arrays,需要以相似的方式更改元素的顺序。 by property c. i should get index of object of property c: 2 for example and place it to the right index in arrB.通过属性 c。我应该获取属性 c 的 object 的索引:例如 2,并将其放在 arrB 中的正确索引中。 JavaScript JavaScript

const arrA= [{
    a: 1,
    b: [{
        c: 2,
        d: 3
      },
      {
        c: 5,
        f: 6
      }
    ]
  },
  {
    a: 2,
    b: [{
        c: 2,
        d: 3
      },
      {
        c: 5,
        f: 6
      }
    ]
  }
];

const arrB = [{
    a: 1,
    b: [{
        c: 5,
        f: 6
      },
      {
        c: 2,
        d: 3
      },
    ]
  },
  {
    a: 2,
    b: [{
        c: 5,
        f: 6
      },
      {
        c: 2,
        d: 3
      },
    ]
  }
];

What I need is to sort arrB in order of all elements in key b to have the same order like in arrA .我需要的是按键b中所有元素的顺序对arrB进行排序,使其与arrA中的顺序相同。 in reality this object contain more fields and they aren't similar.实际上,这个 object 包含更多字段,它们并不相似。 but they have the same property c by which i should change the order但它们具有相同的属性 c 我应该更改顺序

const arrB= [{
    a: 1,
    b: [{
        c: 2,
        d: 3
      },
      {
        c: 5,
        f: 6
      }
    ]
  },
  {
    a: 2,
    b: [{
        c: 2,
        d: 3
      },
      {
        c: 5,
        f: 6
      }
    ]
  }
];

You can use sort() to order the elements based on the indexes of the matching elements in arrA[i].b .您可以使用sort()根据arrA[i].b中匹配元素的索引对元素进行排序。

 const arrA = [{ a: 1, b: [{ c: 2, d: 3 }, { c: 5, f: 6 } ] }, { a: 2, b: [{ c: 2, d: 3 }, { c: 5, f: 6 } ] } ]; const arrB = [{ a: 1, b: [{ c: 5, f: 6 }, { c: 2, d: 3 }, ] }, { a: 2, b: [{ c: 5, f: 6 }, { c: 2, d: 3 }, ] } ]; arrB.forEach((item, i) => item.b.sort((el1, el2) => arrA[i].b.findIndex(el => el.c == el1.c) - arrA[i].b.findIndex(el => el.c == el2.c))); console.log(arrB);

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

相关问题 如何比较一个数组中的元素与Javascript中另一个数组的索引? - how to compare the elements of one array to the index of another array in Javascript? 如何根据javascript中的另一个数组对数组进行排序? - How to sort an array according to another array in javascript? Javascript:如何交换对象数组的元素(通过引用而不是索引)? - Javascript: How can I swap elements of an array of objects (by reference, not index)? javascript-根据另一个数组中的位置仅对数组的某些元素进行排序 - javascript - sort only certain elements of array according to position in another array 如何根据另一个对象数组更新一个对象数组 - How to update an array of objects according another array of objects 如何根据 Javascript 中的属性过滤对象数组 - How to filter an array of objects according to an attribute in Javascript 根据数组中的对象数更改ID(javascript / jquery) - Change id according to the number of objects in an array (javascript/jquery) 如何在javascript中更改对象属性之一传播到另一个对象的数组 - How to change array that one of the objects properties spread in to another in javascript 如何用另一个数组更改对象数组的条目 - How to change entries of Array of Objects with another Array 如何在另一个数组JavaScript中循环索引数组 - How to looping index array in another array JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM