Reputation: 576
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);
Upvotes: 1
Views: 2401
Reputation: 31
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
Upvotes: 0
Reputation: 2340
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)
}
}
Upvotes: 0
Reputation: 7455
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);
Upvotes: 0
Reputation: 6066
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);
Upvotes: 1
Reputation: 39332
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);
Upvotes: 0
Reputation: 2476
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.
This is the way Chrome Developer Console will show arrays to understand array index and corresponding values
Upvotes: 0