1

I have an object or array (i am not sure) that looks like this:

0:{62: "01:30:00 - 01:45:00"}
1:{61: "01:30:00 - 01:45:00"}
2:{55: "03:15:00 - 04:15:00"}
...

My goal is to make it look like this:

62:"01:30:00 - 01:45:00"
61:"01:30:00 - 01:45:00"
...

I need to keep the same order as in the first object this is very important. I've tried this but the result is the exact same.

finalOptions = [];
for (var newKey in newOptions) {
    finalOptions.push(newOptions[newKey]);
}

console.log(finalOptions);
Michael
  • 556
  • 1
  • 8
  • 27
  • If your desire is to have an object with properties named `62`, `61`, `55` *in that order*, then this is a MUST read: [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/q/5525795/1220550) – Peter B Sep 05 '18 at 12:53
  • @MichaelMontero I don't know, how can I know this? – Michael Sep 05 '18 at 12:58

6 Answers6

1

Try this:

var myObject = {
  0:{62: "01:30:00 - 01:45:00"},
  1:{61: "01:30:00 - 01:45:00"},
  2:{55: "03:15:00 - 04:15:00"}
};

var newObject = {};
for (s in myObject) {
  for (ss in myObject[s])
    newObject[ss] = myObject[s][ss];
}

console.log(newObject);

This if you want to keep the orignal order

var myObject = {
  0:{62: "01:30:00 - 01:45:00"},
  1:{61: "01:30:00 - 01:45:00"},
  2:{55: "03:15:00 - 04:15:00"}
};

var newObject = [];
for (s in myObject) {
  for (ss in myObject[s])
    newObject.push(myObject[s][ss]);
}

console.log(newObject);
lviggiani
  • 5,824
  • 12
  • 56
  • 89
  • 1
    Please explain the reason for down voting – lviggiani Sep 05 '18 at 12:48
  • I get a very weird result in my console "createCallback createDelegate etc." nothing at all that looks like the snippet – Michael Sep 05 '18 at 12:53
  • Which browser are you using? If you click on the "Run code snippet", result will appear just below the snippet, not in the browser console – lviggiani Sep 05 '18 at 12:54
  • I am using chrome my starting data is an Array and I want and Object as result, basically I need to remove the array positions and make an object of the results – Michael Sep 05 '18 at 13:19
0

If you have an Array of object you can try:

var ar = [{0: {62: "01:30:00 - 01:45:00"}, 1: {61: "01:30:00 - 01:45:00"},2: { 55: "03:15:00 - 04:15:00" }}]
 
var result;
for (el of ar) {
  for (obj in el) {
    for (text in el[obj]) {
      result = el[obj][text];result 
      console.log(result)
    }
  }
}

But if you have An object, you can try this:

var obj = {
              0: { 62: "01:30:00 - 01:45:00" },
              1: { 61: "01:30:00 - 01:45:00" },
              2: { 55: "03:15:00 - 04:15:00" }
          };
var result;
for (key in obj) {
  for (el in obj[key]) {
    result = obj[key][el];
    console.log(result)
  }
}
michaelitoh
  • 2,317
  • 14
  • 26
0

what you have is an array of objects. What you are trying to do is read objects from array1 (newOptions) and assign to array2 (finalOptions). Results will definitely be same.

enter image description here

This is the way Chrome Developer Console will show arrays to understand array index and corresponding values

Jacob Nelson
  • 2,370
  • 23
  • 35
0

In case you are working with an array of objects, you can use .reduce() method:

let data = [
  {62: "01:30:00 - 01:45:00"},
  {61: "01:30:00 - 01:45:00"},
  {55: "03:15:00 - 04:15:00"}
];

let result = data.reduce((a, c) => {
  let [[k, v]] = Object.entries(c);
  a[k] = v; return a;
}, {});

console.log(result);
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
0

if you want to keep the order and know both key and a value, you can run this code to get an array of objects where key and value are specified and order is the same.

const initial = [
        {62: "01:30:00 - 01:45:00"},
        {61: "01:30:00 - 01:45:00"},
        {55: "03:15:00 - 04:15:00"}];

    const finalOptions = [];
    for (let i=0; i<initial.length; i++) {
        let option = initial[i];
        let key = Object.keys(option)[0]; // get first and only key from option
        finalOptions.push({key:key, value: option[key]});
    }
    
    console.log(finalOptions);
Artem Arkhipov
  • 7,025
  • 5
  • 30
  • 52
0

It's best to iterate over objects if they are arrays so first you can do

   const myObject =  {
     0:{62: "01:30:00 - 01:45:00"},
     1:{61: "01:30:00 - 01:45:00"},
     2:{55: "03:15:00 - 04:15:00"}
   };

   const objToArray = Object.values(myObject);  //makes the array
   const objYouWant = {}; //Initializes place to build your object

  //iterates over array you just made translating each index into a key-value pair
   const backToObject = objToArray.map((item => {
    return objYouWant[item.key] = item.value
   })

If you console.log objYouWant you should get

   {
     62: ""01:30:00 - 01:45:00",
     61: "01:30:00 - 01:45:00",
     55: "03:15:00 - 04:15:00"
   }

Which is I think, what you want

Trevor
  • 21
  • 1
  • 4