gpt4 book ai didi

JavaScript - 2D 数组/对象

转载 作者:行者123 更新时间:2023-12-03 07:55:27 27 4
gpt4 key购买 nike

我正在尝试创建数组和/或对象来保存用于绘图的值。数据以 JSON 形式出现。每个对象都有一个 Assets 代码、日期和我感兴趣的两个测量值。(测量值 3 是两者的净值)我尝试按 Assets 代码和捕获数据的日期进行过滤。我想将其编译成一个对象,我可以循环遍历并按日期提取每个 Assets 的数据。我的尝试是创建一个数组数组,但遇到了一些 undefined variable 问题。有没有更简洁的方法来实现这一点?

var response = []; //to store global variables

response.handle = function handle (resp) {
response.measurment1 = {};
response.measurmentt2 = {};
response.measurement3 = {};

var dateArray = [];
var assetCodeArray = [];

for(object in resp) {
if (extend.contains(dateArray,resp[object].date == false){ // extend.contains is a function that checks if an obj is in an array
dateArray.push(resp[object].date;
}
if (extend.contains(dateArray,resp[object].AssetCode == false){ // extend.contains is a function that checks if an obj is in an array
assetCodeArray.push(resp[object].date;
}
}
for (var code in assetCodeArray) {
response.injObj[assetCodeArray[code]] = [];
response.WthObj[assetCodeArray[code]] = [];
response.netObj[assetCodeArray[code]] = [];

var filteredRespAC = resp.filter(function(y) {
return y.AssetCode == assetCodeArray[code];
});
var AssetCodeInj = 0;
var AssetCodeWth = 0;
var AssetCodeNet = 0;
for (var obj in filteredRespAC) {
for (date in dateArray) {
var filterACDate = filteredRespAC.filter(function(z) {
return z.date == dateArray[date];
});

var volInj = filterACDate[date]["Measurement1"];
var volWth = filterACDate[date]["measurment2"];
var volNet = volInj + volWth;

response.injObj[assetCodeArray[code]].push(volInj);
response.WthObj[assetCodeArray[code]].push(volWth);
response.netObj[assetCodeArray[code]].push(volNet);
}
}
<小时/>

JSON 的表示如下:

[
{
"ID" : 12345,
"AssetCode" : "AC1",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12346,
"AssetCode" : "AC1",
"Measure1" : 6789,
"Measure2" : -678,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12347,
"AssetCode" : "AC2",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-14T00:00:00"
},{
"ID" : 12348,
"AssetCode" : "AC1",
"Measure1" : 12345,
"Measure2" : -1234,
"Date" : "2016-01-13T00:00:00"
}
]

最佳答案

我认为这至少是您想要的一部分。

data.reduce(function (last, current, index, array) {
if (last[current.AssetCode] == undefined) {
console.log(last);
last[current.AssetCode] = array.filter(function (value) {
return current.AssetCode === value.AssetCode;
});
return last;
}
return last;
}, {});

此函数将您的 JSON(修复花括号后)转换为此。

{
AC1: [{
AssetCode: "AC1",
Date: "2016-01-14T00:00:00",
ID: 12345,
Measure1: 12345,
Measure2: -1234
}, {
AssetCode: "AC1",
Date: "2016-01-14T00:00:00",
ID: 12346,
Measure1: 6789,
Measure2: -678
}, {
AssetCode: "AC1",
Date: "2016-01-13T00:00:00",
ID: 12348,
Measure1: 12345,
Measure2: -1234
}],
AC2: [{
AssetCode: "AC2",
Date: "2016-01-14T00:00:00",
ID: 12347,
Measure1: 12345,
Measure2: -1234
}]
}

关于JavaScript - 2D 数组/对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34817751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com