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

时间:2018-09-05 12:39:43

标签: javascript arrays object

我有一个看起来像这样的对象或数组(我不确定):

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

我的目标是使其看起来像这样:

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

我需要保持与第一个对象相同的顺序,这非常重要。 我已经尝试过了,但是结果是完全一样的。

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

console.log(finalOptions);

6 个答案:

答案 0 :(得分:0)

尝试一下:

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

如果您想保持原始秩序,则此

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

答案 1 :(得分:0)

如果您有对象数组,可以尝试:

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

但是,如果您有An对象,则可以尝试以下操作:

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 :(得分:0)

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

enter image description here

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

答案 3 :(得分:0)

如果要使用对象数组,则可以使用.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);

答案 4 :(得分:0)

如果要保持顺序并同时知道键和值,则可以运行此代码以获取一个对象数组,其中指定了键和值,并且顺序相同。

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)

如果对象是数组,则最好对其进行迭代,因此首先可以这样做

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

如果您console.log objYouWant,您应该得到

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

我在想什么,你想要什么