我有一个键形式的字符串数组:由“ :: ”分隔的值,值可以有多行。我想知道如何创建将键与特定字符串匹配的哈希表并映射值并从中构建 JSON 文件?抱歉,如果它已经得到回答,但我整天都在搜索,但找不到实现这一目标的方法。谢谢,滴滴
问问题
143 次
3 回答
0
I din't really get what you want exactly. But from what I understood, this should be what you want. Could you provide some snippets ?
var myObject = {
pro1 : "value1",
prop2: "value2"
}
JSON.stringify(myObject); // outputs the object as json
于 2013-03-27T20:16:01.550 回答
0
Something like
var stringsArray=['key1::value1','key2::value2','key3::value3'.....]
var hashObj={};
for(var i=0; i<stringsArray.length; i++){
var kv=stringsArray[i].split('::')
hashObj[kv[0]]=kv[1];
}
alert(hashObj.key1)
This will only work for unique key values
于 2013-03-27T20:17:44.533 回答
0
例如字符串 = ["a::1", "b::2", "c::3"]。你能做的是。
let output = {};
for (let i = 0; i < strings.length; ++i)
{
let key = strings[i].split("::")[0];
let value = strings[i].split("::")[1];
output[key] = value;
}
于 2013-03-27T20:20:12.270 回答