如何删除值并向JavaScript对象添加索引列?

时间:2022-04-12 07:35:32

I have this data coming in:

我得到的数据是

[]
0:{time: 1525355921817, sym: "AAPL", price: 169.16, size: 98, stop: false, …}
1:{time: 1525355923216, sym: "AAPL", price: 170.15, size: 6, stop: false, …}
2:{time: 1525355923216, sym: "AAPL", price: 170.06, size: 57, stop: false, …}

I need to get it into the following format:

我需要把它做成如下格式:

0:{"time": 1525355921817, "sym": "AAPL", "price": 169.16, "index": 0} 
1:{"time": 1525355923216, "sym": "AAPL", "price": 170.15, "index": 1} 
2:{"time": 1525355923216, "sym": "AAPL", "price": 170.06, "index": 2}

Basically just dropping the size and stop and adding an index column. I also need to be able to access it as this.data.price and get back all the price values... is this even possible? This data comes from a service in angular

基本上就是去掉大小,停止添加索引列。我还需要能够访问它。把所有的价格都拿回来……这是可能吗?这些数据来自于一个有角的服务。

How can I achieve this? (please be aware that I am not an experienced user of angular or javascript)

我如何做到这一点?(请注意,我并不是一个有过使用角或javascript经验的用户)

3 个解决方案

#1


4  

You can map the array of objects. You can deconstruct each object for shorter code.

您可以映射对象数组。您可以解构每个对象以获得更短的代码。

let arr = [{time: 1525355921817, sym: "AAPL", price: 169.16, size: 98, stop: false,},{time: 1525355923216, sym: "AAPL", price: 170.15, size: 6, stop: false,},{time: 1525355923216, sym: "AAPL", price: 170.06, size: 57, stop: false,}];

let result = arr.map(({size,stop,...r}, i) => Object.assign(r, {index: i}));

console.log( result );

Doc: map(), Destructuring Assignment

医生:地图()、解构的任务

#2


1  

You can use .map() to create the desired ouput:

可以使用.map()创建所需的输出:

let data = [{time: 1525355921817, sym: "AAPL", price: 169.16, size: 98, stop: false},{time: 1525355923216, sym: "AAPL", price: 170.15, size: 6, stop: false},{time: 1525355923216, sym: "AAPL", price: 170.06, size: 57, stop: false}];

let result = data.map((o, i) => ({
  time: o.time,
  sym: o.sym,
  price: o.price,
  index: i
}));

console.log(result);

#3


0  

I also need to be able to access it as this.data.price and get back all the price values... is this even possible?

我还需要能够访问它。把所有的价格都拿回来……这是可能吗?

Assuming the array is under data:

假设数组在数据下:

 const groupValue = (arr, key) => arr[key] = arr.map(el => el[key]);

 groupValue(this.data, "price");

 console.log(this.data.price);

#1


4  

You can map the array of objects. You can deconstruct each object for shorter code.

您可以映射对象数组。您可以解构每个对象以获得更短的代码。

let arr = [{time: 1525355921817, sym: "AAPL", price: 169.16, size: 98, stop: false,},{time: 1525355923216, sym: "AAPL", price: 170.15, size: 6, stop: false,},{time: 1525355923216, sym: "AAPL", price: 170.06, size: 57, stop: false,}];

let result = arr.map(({size,stop,...r}, i) => Object.assign(r, {index: i}));

console.log( result );

Doc: map(), Destructuring Assignment

医生:地图()、解构的任务

#2


1  

You can use .map() to create the desired ouput:

可以使用.map()创建所需的输出:

let data = [{time: 1525355921817, sym: "AAPL", price: 169.16, size: 98, stop: false},{time: 1525355923216, sym: "AAPL", price: 170.15, size: 6, stop: false},{time: 1525355923216, sym: "AAPL", price: 170.06, size: 57, stop: false}];

let result = data.map((o, i) => ({
  time: o.time,
  sym: o.sym,
  price: o.price,
  index: i
}));

console.log(result);

#3


0  

I also need to be able to access it as this.data.price and get back all the price values... is this even possible?

我还需要能够访问它。把所有的价格都拿回来……这是可能吗?

Assuming the array is under data:

假设数组在数据下:

 const groupValue = (arr, key) => arr[key] = arr.map(el => el[key]);

 groupValue(this.data, "price");

 console.log(this.data.price);