Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!