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

时间:2018-05-06 12:48:45

标签: javascript arrays json angular typescript

我有这些数据:

[]
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, …}

我需要将其改为以下格式:

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}

基本上只是删除大小并停止并添加索引列。我还需要能够以this.data.price的形式访问它并获取所有价格值......这甚至可能吗?此数据来自角度

的服务

我怎样才能做到这一点? (请注意,我不是角色或javascript的有经验的用户)

3 个答案:

答案 0 :(得分:4)

您可以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);对象数组。您可以解构每个对象以获得更短的代码。



map




文档:map()Destructuring Assignment

答案 1 :(得分:1)

您可以使用static void ReadFolderFiles() { // The parent folder of "my_folder" will be **exe**'s folder: var dir = new DirectoryInfo("my_folder\\sub_folder"); // Or: DirectoryInfo("my_folder") foreach(FileInfo file in dir.EnumerateFiles()) { System.Console.WriteLine(file.Name); } } 创建所需的输出:



.map()




答案 2 :(得分:0)

  

我还需要能够以this.data.price的形式访问它并获取所有价格值......这甚至可能吗?

假设数组位于data

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

 groupValue(this.data, "price");

 console.log(this.data.price);