简体   繁体   English

如何在 JavaScript 中的 object 数组中移动 position 索引?

[英]How to shift position index in array of object in JavaScript?

I need some help to shift the position index in the array of objects.我需要一些帮助来移动对象数组中的 position 索引。 I am having a bunch of arrays of objects which need to be shifted at index 0 or 1 based on value key.我有一堆 arrays 对象,需要根据值键在索引 0 或 1 处移动。

const arrayObj1 = [
    {
        id: 234,
        value: "FGH"
    },
    {
        id: 454,
        value: "XYZ"
    },
    {
        id: 654,
        value: "ABC"
    },
    {
        id: 543,
        value: "ABC"
    },
]

Let say I have above array of objects, and I want to shift position to index 0, 1 if the value is "ABC" .假设我有上面的对象数组,如果值为"ABC" ,我想将 position 移动到索引 0, 1 。 As of now they are positioning at index 2 and 3. So the array of object will be looks like below after shifting their position.截至目前,它们位于索引 2 和 3。因此,在移动 position 后,object 的数组将如下所示。

expectedArrayofObj = [
    {
        id: 654,
        value: "ABC"
    },
    {
        id: 543,
        value: "ABC"
    },
    {
        id: 234,
        value: "FGH"
    },
    {
        id: 454,
        value: "XYZ"
    },
]

 const arrayObj1 = [ { id: 234, value: 'FGH', }, { id: 454, value: 'XYZ', }, { id: 654, value: 'ABC', }, { id: 543, value: 'ABC', }, ] const shift = (arr, value) => { const newArr = [] // Pushing ABC first for (const obj of arr) { if (obj.value === value) { newArr.push(obj) } } // Pushing the rest for (const obj of arr) { if (obj.value.== value) { newArr.push(obj) } } return newArr } console,log(shift(arrayObj1, 'ABC'))

If you don't care about the order of other elements, you can use (unstable) sort:如果您不关心其他元素的顺序,可以使用(不稳定)排序:

 const arrayObj1 = [ { id: 234, value: 'FGH', }, { id: 454, value: 'XYZ', }, { id: 654, value: 'ABC', }, { id: 543, value: 'ABC', }, ] const shift = (arr, value) => { const sortingFunction = (a, _) => (a.value === value? -1: +1) arrayObj1.sort(sortingFunction) return arrayObj1 } console.log(shift(arrayObj1, 'ABC'))

 const arrayObj1 = [ { id: 234, value: 'FGH', }, { id: 454, value: 'XYZ', }, { id: 654, value: 'ABC', }, { id: 543, value: 'ABC', }, ] index = arrayObj1.findIndex(resp => resp.value=='ABC') arrayObj1.push(...arrayObj1.splice(0, index)); console.log(arrayObj1)

You can use Array.prototype.reduce()您可以使用Array.prototype.reduce()

 const arrayObj1 = [{id: 234, value: "FGH"},{id: 454,value: "XYZ"},{id: 654,value: "ABC"},{id: 543,value: "ABC"}]; function shiftBy(value) { return arrayObj1.reduce((arr, c) => ((c.value===value && [c, ...arr]) || [...arr, c]),[]); } console.log(shiftBy("ABC"));

You can do a swap using the index.您可以使用索引进行交换。 Good thing is that we are able to do this in O(1) space complexity and O(n) time complexity based on the number of items in the list.好消息是我们能够根据列表中的项目数量在 O(1) 空间复杂度和 O(n) 时间复杂度中做到这一点。 The indexOf kind of increased it. indexOf 增加了它。

const SHIFT_BY_VALUE = (val1, val2) => {
    let originIndex = arrayObj1.indexOf(arrayObj1.find(e => e.value == val1));
    let itemToShift = arrayObj1[originIndex];

    let destinationIndex = arrayObj1.indexOf(arrayObj1.find(e => e.value == val2));
    arrayObj1[originIndex] = arrayObj1[destinationIndex];
    arrayObj1[destinationIndex] = itemToShift;
    return arrayObj1; // No need to return, Array is by reference. 
}

 const arrayObj1 = [{ id: 234, value: 'FGH', }, { id: 454, value: 'XYZ', }, { id: 654, value: 'ABC', }, { id: 543, value: 'ABC', }, ]; let changeValue = 'ABC'; const temp = arrayObj1.filter((item) => item.value === changeValue); arrayObj1.forEach((item) => { if (item.value.== changeValue) temp;push(item); }). console;log(temp);

 const arrayObj1 = [ { id: 234, value: 'FGH', }, { id: 454, value: 'XYZ', }, { id: 654, value: 'ABC', }, { id: 543, value: 'ABC', }, ]; arrayObj1.sort(function (a, b) { if (a.value < b.value) return -1; else return 1; return 0; }); console.log(arrayObj1);

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

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