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);
2
  • 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? Commented Sep 5, 2018 at 12:53
  • @MichaelMontero I don't know, how can I know this? Commented Sep 5, 2018 at 12:58

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Please explain the reason for down voting
I get a very weird result in my console "createCallback createDelegate etc." nothing at all that looks like the snippet
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
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
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

Comments

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

2 Comments

this code gives me an object with 0: "3" in it as only result
You are not answering the question, you just sorted by key but not removed the first key..
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);

3 Comments

I need an object as result not an array
Iteration order is not specified for objects in javascript. You are not able to create such. What is the reason to use object?
You are not answering the question, you just sorted by key but not removed the first key. the initial array is wrong.
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)
  }
}

2 Comments

I don't understand why you do a console.log? I need an abject of all the items as result
I just print what you call result. You can do whatever you want with that. I've updated my answer anyway.
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

1 Comment

it just does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.