简体   繁体   English

如何对未排序的对象数组的 OBJECT 中的元素重新排序?

[英]How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort?

Every search for how to reorder (NOT SORT) an element inside of an object of an array of objects keeps returning how to SORT an array.每次搜索如何对对象数组的 object 中的元素进行重新排序(不排序)时,都会返回如何对数组进行排序。 That's not what I want.那不是我想要的。

Here's a quit and dirty example:这是一个退出和肮脏的例子:

let obj = [
  {
     name: "hi number 1a",
     id: "hi number 2a",
     address: "hi number 3a"
  },  {
     name: "hi number 1b",
     id: "hi number 2b",
     address: "hi number 3b"
  },  {
     name: "hi number 1c",
     id: "hi number 2c",
     address: "hi number 3c"
  },  {
     name: "hi number 1d",
     id: "hi number 2d",
     address: "hi number 3d"
  }
]

What I want to do is LOOP through all the objects and REORDER them so id is NOW in name's position for all of them, like so:我想要做的是遍历所有对象并重新排序它们,因此 id 现在在名称的 position 中为所有对象,如下所示:

let obj = [
  {
     id: "hi number 2a",
     name: "hi number 1a",
     address: "hi number 3a"
  },  {
     id: "hi number 2b",
     name: "hi number 1b",
     address: "hi number 3b"
  },  {
     id: "hi number 2c",
     name: "hi number 1c",
     address: "hi number 3c"
  },  {
     id: "hi number 2d",
     name: "hi number 1d",
     address: "hi number 3d"
  }
]

Here's the code I've tried:这是我尝试过的代码:

        $scope.arraymove = (arr, fromindex, toindex) => {

            let len = arr.length;
            let newArr = [];
            let resultArr = [];

            for (let i = 0; i < len; i++) {
                resultArr[i] = move(arr[i], fromindex, toindex);
                newArr.push(resultArr[i]);
            }

            function move(thearr, old_index, new_index) {
                while (old_index < 0) {
                    old_index += len;
                }
                while (new_index < 0) {
                    new_index += len;
                }
                if (new_index >= len) {
                    var k = new_index - len;
                    while ((k--) + 1) {
                        thearr.push(undefined);
                    }
                }
                thearr.splice(new_index, 0, thearr.splice(old_index, 1)[0]); // CODE DIES HERE

                console.log("NEW ARRAY: ", thearr);
                return thearr;
            }

            console.log("New Reordered ARRAY: ", newArr);
            return newArr;
        }

What happens is that the INCOMING arr loses SCOPE and when it hits splice, I get SPLICE is a function.发生的情况是传入的 arr 丢失了 SCOPE,当它遇到拼接时,我得到 SPLICE 是 function。

Yes, I'm working for a LARGE telecom STILL using AngularJS.是的,我仍在使用 AngularJS 为大型电信公司工作。 Anyway, it's simply pure javascript at this point.无论如何,此时它只是纯粹的 javascript。

Here you go, though be aware that object field order cannot be guaranteed.这里是 go,但请注意,不能保证 object 字段顺序。

 let obj = [ { name: "hi number 1a", id: "hi number 2a", address: "hi number 3a" }, { name: "hi number 1b", id: "hi number 2b", address: "hi number 3b" }, { name: "hi number 1c", id: "hi number 2c", address: "hi number 3c" }, { name: "hi number 1d", id: "hi number 2d", address: "hi number 3d" } ] let newobj = obj.map( o => ({id: o.id, name: o.name, address: o.address})); console.log(newobj)

The properties within JavaScript objects have no explicit order. JavaScript 对象中的属性没有明确的顺序。 Typically though, they're represented in the order they are added.不过,通常情况下,它们按添加顺序表示。 "Rearranging" the properties seems more like an OCD adventure than anything. “重新排列”这些属性似乎更像是一场强迫症冒险,而不是任何事情。 There isn't a "position 0".没有“位置 0”。

That said, if I were on this OCD quest, I'd do something really simple:也就是说,如果我在这个强迫症任务中,我会做一些非常简单的事情:

let obj = [
  {
     name: "hi number 1a",
     id: "hi number 2a",
     address: "hi number 3a"
  },  {
     name: "hi number 1b",
     id: "hi number 2b",
     address: "hi number 3b"
  },  {
     name: "hi number 1c",
     id: "hi number 2c",
     address: "hi number 3c"
  },  {
     name: "hi number 1d",
     id: "hi number 2d",
     address: "hi number 3d"
  }
];

obj = obj.map(function(instance){
    return {
        id: instance.id,
        name: instance.name,
        address: instance.address
    }
});

Please note that this will replace your objects with new objects that have the desired order.请注意,这会将您的对象替换为具有所需顺序的新对象。 If your application has a reference to any of the old objects, this may be undesirable.如果您的应用程序引用了任何旧对象,这可能是不可取的。

If you wish to keep the object references intact, you could instead forEach() your array, copy the properties, delete them, and reassign.如果您希望保持 object 引用完整,您可以改为 forEach() 您的数组,复制属性,删除它们,然后重新分配。 Something like this:像这样的东西:

obj.forEach(function(instance){
   let temp = {
        id: instance.id,
        name: instance.name,
        address: instance.address
    };
    delete instance.id;
    delete instance.name;
    delete instance.address;
    instance.id = temp.id;
    instance.name = temp.name;
    instance.address = temp.address;
});

Once again though, this all seems unnecessary.再一次,这一切似乎都是不必要的。 The only case I can think of where this might make sense is if you need to pass your data to some process that actually cares about the "order" of non-ordinal properties (which seems broken and fragile).我能想到的唯一可能有意义的情况是,如果您需要将数据传递给某个真正关心非序数属性的“顺序”的进程(这似乎是破碎和脆弱的)。

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

相关问题 如何通过对象内的参数对对象数组进行排序? - How can I sort a array of objects by a parameter inside an objects? 如何按对象值对对象数组进行排序? - How can I sort an array of objects by the object value? 如何通过数组内的“sortby”键对这个对象进行排序? - How can I sort this object by 'sortby' key which inside an array? Javascript 如果 object 在数组中,我如何调用 object 的元素? - Javascript how can I call an element of an object if the object is inside an array? 如何在JavaScript中重新排序/排序NodeList? - How can I reorder/sort a NodeList in JavaScript? 如何循环遍历 object 数组并从数组中列出的两个对象中获取一个属性元素? - How can I loop through an array of object and get one property element out of the two objects listed inside the array? 如何获取 object 中数组元素的索引? - How can I get the index of array element inside object? 如何对存储在对象中的数组内的对象进行排序 - How to sort objects inside an array which is stored in an object 如何用内部包含多个对象的新数组替换 object 中的数组? - How can i replace an Array inside of an object with new array with multiple objects inside? 如何根据每个 object 中的数组从 object 数组中返回某些对象? - How can I return certain objects from object array based on an array inside each object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM