JavaScript删除对象的键并保持相同的顺序

时间:2022-12-20 04:45:21

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

5 个解决方案

#1


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.

你拥有的是一系列对象。您要做的是从array1(newOptions)读取对象并分配给array2(finalOptions)。结果肯定是一样的。

JavaScript删除对象的键并保持相同的顺序

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

这是Chrome开发者控制台显示数组以了解数组索引和相应值的方式

#2


0  

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

如果您正在使用对象数组,则可以使用.reduce()方法:

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

#3


0  

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

#4


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

#5


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

#1


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.

你拥有的是一系列对象。您要做的是从array1(newOptions)读取对象并分配给array2(finalOptions)。结果肯定是一样的。

JavaScript删除对象的键并保持相同的顺序

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

这是Chrome开发者控制台显示数组以了解数组索引和相应值的方式

#2


0  

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

如果您正在使用对象数组,则可以使用.reduce()方法:

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

#3


0  

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

#4


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

#5


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