gpt4 book ai didi

javascript - 将一组未删节的连续字符串值解析为可用的可索引键值对

转载 作者:行者123 更新时间:2023-11-30 10:57:15 25 4
gpt4 key购买 nike

This is NOT a duplicate .我已经尝试过 JSON.parse() 但它不起作用。
这是我的问题:this crucial & helpful answer I got yesterday 中提供的代码产生一个我无法从中提取单个值的结果,因为它显示为一个值的单个连续字符串:

这段代码

    var result = {};
for ( var i = 0; i < arr.length; i++ ) {
var key = "asset: " + arr[ i ].geo + ' ym: ' + arr[ i ].ym + ' venue: ' + arr[ i ].venue + ' value';
result[ key ] = (result[ key ] || 0 ) + arr[ i ].value ;
}
Logger.log(result);

转换这个数组

    arr = [
{value:1.0, venue: "binance", ym:20181.0, geo:"eur"},
{value:6.0, venue: "binance", ym:20181.0, geo:"eur"},
{value:2.0, venue: "bitstamp", ym:20181.0, geo:"eur"},
{value:5.0, venue: "binance", ym:20182.0, geo:"eur"},
{value:1.0, venue: "binance", ym:20183.0, geo:"eur"},
{value:3.0, venue: "binance", ym:20181.0, geo:"usd"},
{value:3.0, venue: "binance", ym:20182.0, geo:"usd"},
{value:3.0, venue: "binance", ym:20183.0, geo:"usd"},
{value:2.0, venue: "binance", ym:20181.0, geo:"pop"},
{value:2.0, venue: "binance", ym:20182.0, geo:"pop"},
{value:2.0, venue: "binance", ym:20183.0, geo:"pop"},
{value:2.0, venue: "binance", ym:20181.0, geo:"dot"},
{value:2.0, venue: "binance", ym:20182.0, geo:"dot"}
];

进入这个连续的数据串

{asset: eur ym: 20181 venue: bitstamp value=2.0, asset: eur ym: 20182 venue: binance value=5.0, asset: dot ym: 20183 venue: binance value=2.0, asset: eur ym: 20183 venue: binance value=1.0, asset: usd ym: 20181 venue: binance value=3.0, asset: sol ym: 20183 venue: binance value=1.0, asset: pop ym: 20183 venue: binance value=2.0, asset: dot ym: 20182 venue: binance value=2.0, asset: dot ym: 20181 venue: binance value=2.0, asset: usd ym: 20183 venue: binance value=3.0, asset: sol ym: 20181 venue: binance value=1.0, asset: usd ym: 20182 venue: binance value=3.0, asset: cad ym: 20183 venue: binance value=3.0, asset: sol ym: 20182 venue: binance value=1.0, asset: pop ym: 20182 venue: binance value=2.0, asset: pop ym: 20181 venue: binance value=2.0, asset: cad ym: 20181 venue: binance value=3.0, asset: cad ym: 20182 venue: binance value=3.0, asset: eur ym: 20181 venue: binance value=7.0}

我不知道如何使用它,因为我无法从中提取特定值,就像我在标准数组中所做的那样,例如 result[i].valueresult[i].ym 返回一个特定的key-value集合。
在这里它似乎是一个连续体,其中挑选特定部分对我来说并不容易。
即使 Logger.log(typeof result); 返回 object 我仍然无法从中提取任何值。我非常感谢这个代码的提供者,我知道这肯定是我的错,这可能是因为我缺乏经验,但目前我不知道如何处理这个结果。

最佳答案

这是一个 Google Apps 脚本解决方案。

它读取数组并计算每周总计,然后在事件电子表格上输出输入数组和每周结果。

function sumArray() {
var arr=[{value:1.0, venue: "binance", ym:20181.0, geo:"eur"},{value:6.0, venue: "binance", ym:20181.0, geo:"eur"},{value:2.0, venue: "bitstamp", ym:20181.0, geo:"eur"}, {value:5.0, venue: "binance", ym:20182.0, geo:"eur"}, {value:1.0, venue: "binance", ym:20183.0, geo:"eur"}, {value:3.0, venue: "binance", ym:20181.0, geo:"usd"}, {value:3.0, venue: "binance", ym:20182.0, geo:"usd"}, {value:3.0, venue: "binance", ym:20183.0, geo:"usd"}, {value:2.0, venue: "binance", ym:20181.0, geo:"pop"}, {value:2.0, venue: "binance", ym:20182.0, geo:"pop"}, {value:2.0, venue: "binance", ym:20183.0, geo:"pop"}, {value:2.0, venue: "binance", ym:20181.0, geo:"dot"}, {value:2.0, venue: "binance", ym:20182.0, geo:"dot"}];
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
sh.clearContents();//clear active sheet
sh.appendRow(['value','venue','ym','geo']);//append headers for array
arr.forEach(function(e,i){
sh.appendRow([e.value,e.venue,e.ym,e.geo]);
});//append array
var vObj={};
arr.forEach(function(e,i){
if(!vObj.hasOwnProperty(e.venue)) {vObj[e.venue]={};}
if(!vObj[e.venue].hasOwnProperty(e.ym)) {vObj[e.venue][e.ym]=e.value;}else{vObj[e.venue][e.ym]+=e.value;}
if(!vObj.hasOwnProperty(e.geo)) {vObj[e.geo]={};}
if(!vObj[e.geo].hasOwnProperty(e.ym)) {vObj[e.geo][e.ym]=e.value;}else{vObj[e.geo][e.ym]+=e.value;}
if(!vObj.hasOwnProperty('ym')) {vObj['ym']={};}
if(!vObj['ym'].hasOwnProperty(e.ym)) {vObj['ym'][e.ym]=e.value;}else{vObj['ym'][e.ym]+=e.value;}
});//calculate week sums
//Logger.log(JSON.stringify(vObj));
var keys=Object.keys(vObj.ym);
keys.unshift('')
sh.appendRow(keys);//headers for weekly sums
var vkeys=Object.keys(vObj);
//Logger.log(vkeys);
for(var i=0;i<vkeys.length;i++) {
var row=[];
row.push(vkeys[i]);
var rkeys=Object.keys(vObj[vkeys[i]]);
for(var j=1;j<keys.length;j++) {
if(vObj[vkeys[i]].hasOwnProperty(keys[j])) {
row.push(vObj[vkeys[i]][keys[j]]);
}else{
row.push('');
}
}
sh.appendRow(row);//appending weekly sum
}
}

这是输出的图像:

enter image description here

关于javascript - 将一组未删节的连续字符串值解析为可用的可索引键值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522987/

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