简体   繁体   中英

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. 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:

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.

Yes, I'm working for a LARGE telecom STILL using AngularJS. Anyway, it's simply pure javascript at this point.

Here you go, though be aware that object field order cannot be guaranteed.

 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. 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".

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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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