Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析JSON并创建hashmap_Javascript - Fatal编程技术网

Javascript 解析JSON并创建hashmap

Javascript 解析JSON并创建hashmap,javascript,Javascript,我做了一个解析JSON并创建hashmap的函数,以便在某些操作中使用我的数据。我发现的问题是,我的键被重复了好几次,在hashmap中,每个键只有一个值。我希望它是这样的,就像这个例子: 12345 : 5 12346 : 4 12346 : 3 我的函数返回以下内容:{12345:512346:3} 因此,要恢复,它会用最后一个值重载键 在我的例子中,我需要所有这些值,所以我希望得到如下结果: 结果=> { 12345 : 5 , 12346 : [4,3] } 目标:如果我找到另一个键

我做了一个解析JSON并创建hashmap的函数,以便在某些操作中使用我的数据。我发现的问题是,我的键被重复了好几次,在hashmap中,每个键只有一个值。我希望它是这样的,就像这个例子:

12345 : 5
12346 : 4
12346 : 3
我的函数返回以下内容:
{12345:512346:3}
因此,要恢复,它会用最后一个值重载键

在我的例子中,我需要所有这些值,所以我希望得到如下结果:

结果=>

{ 12345 : 5 , 12346 : [4,3] }
目标:如果我找到另一个键,我应该将该值更改为数组,而不是使其过载

以下是我的功能:

getFacingCount: function(iSceneGraph){
                var facingArray = {};
                if (iSceneGraph.children.length > 0){
                    for (var i = 0; i < iSceneGraph.children.length; i++){
                        facingArray = Object.assign(facingArray, this.getFacingCount(iSceneGraph.children[i]));
                    }
                }
                if (iSceneGraph.merch.type === "Facing"){
                    facingArray[iSceneGraph.merch.ean] = iSceneGraph.merch.hcount;
                }
                return facingArray;
            }
getFacingCount:函数(iSceneGraph){
var facingArray={};
如果(iSceneGraph.children.length>0){
for(var i=0;i
您正在覆盖此处的值:

if (iSceneGraph.merch.type === "Facing"){
    facingArray[iSceneGraph.merch.ean] = iSceneGraph.merch.hcount;
}
您需要将其更改为(基于您想要的):第一个解决方案

但在我看来,我更希望您拥有一个一致的hashmap数据结构,就像Jonas所说的,您的解决方案将是这样的:第二个解决方案

如果hashmap数据具有一致的结构,则可以更轻松地处理数据。看我的第一个解,它有三个条件,而第二个解只需要两个条件。一致的数据结构允许您不去考虑“应该如何处理数据中所有可能的类型”

------更新-----------------

回答您的反馈,如果您现在想要推送数组而不是整数,您可以更改

// `positionList` is an array of integer
facingArray[iSceneGraph.merch.ean].push(positionList);
进入


结果将是以下格式:
{12345:[1,2,3],12346:[4,5,6,7,8,9]}

假设您的JSON看起来像一个对象数组:
[{12345:5},{12346:4},{12346:3}]
您可以使用:

var json=[{12345:5},{12346:4},{12346:3}],
结果=json.reduce(函数(acc、obj){
var key=Object.key(obj);
acc[key]=!acc[key]?obj[key]:[].concat(acc[key],obj[key]);
返回acc;
}, {});

控制台日志(结果)
我建议hashmap数据有一个一致的结构,例如12345:[5],12346:[3,4],这样你就可以一直使用hashmap[12345]。Foreachy你现在需要有这样的结果:
{12345:[1,2,3]],12346:[4,5,6],[7,8,9]}
或者这种结果
{12345:[1,2,3],12346:[4,5,6,7,8,9]}{12345:512346:412346:3}
,这就是为什么您会得到这样的结果:
{12345:512346:3}
我的答案中假设您的JSON看起来像一个对象数组:
[{12345:5},{12346:4},{12346:3}]
。。。。你能发布你的数据吗?这不是我想要的,我甚至没有这些值。。检查我的函数这是JSON的结构:
[{12345:5},{12346:4},{12346:3}]
if (iSceneGraph.merch.type === "Facing"){
    if (facingArray[iSceneGraph.merch.ean] == undefined || facingArray[iSceneGraph.merch.ean] == null) {
        facingArray[iSceneGraph.merch.ean] = [];
    }

    facingArray[iSceneGraph.merch.ean].push(iSceneGraph.merch.hcount);
}
// `positionList` is an array of integer
facingArray[iSceneGraph.merch.ean].push(positionList);
facingArray[iSceneGraph.merch.ean] = facingArray[iSceneGraph.merch.ean].concat(positionList);