最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - How to transform JavaScript hashmap? - Stack Overflow

programmeradmin4浏览0评论

i'm trying to create a <String, Array()> map from a json object. Imagine i got this json structure:

[
    {
        "userId": "123123",
        "password": "fafafa",
        "age": "21"
    },
    {
        "userId": "321321",
        "password": "nana123",
        "age": "34"
    }
]

The map i want to create would be:

key (string), value (array)

{
    "userId": [
        "123123",
        "321321"
    ],
    "password": [
        "fafafa",
        "nana123"
    ],
    "age": [
        "21",
        "34"
    ]
}

Is it possible to do this? :/

Thanks in advance.

i'm trying to create a <String, Array()> map from a json object. Imagine i got this json structure:

[
    {
        "userId": "123123",
        "password": "fafafa",
        "age": "21"
    },
    {
        "userId": "321321",
        "password": "nana123",
        "age": "34"
    }
]

The map i want to create would be:

key (string), value (array)

{
    "userId": [
        "123123",
        "321321"
    ],
    "password": [
        "fafafa",
        "nana123"
    ],
    "age": [
        "21",
        "34"
    ]
}

Is it possible to do this? :/

Thanks in advance.

Share Improve this question edited Sep 17, 2013 at 13:03 Nowaker 12.4k4 gold badges58 silver badges64 bronze badges asked Feb 19, 2013 at 16:57 msqarmsqar 3,0206 gold badges52 silver badges99 bronze badges 7
  • There is no such thing as a "json object" and I don't see what can be the object you display. Is that a string you receive ? – Denys Séguret Commented Feb 19, 2013 at 16:58
  • Best to explain what you want to do with that map, because it may be that you can just use the object. – Popnoodles Commented Feb 19, 2013 at 16:58
  • 2 Your "JSON structure" is not JSON at all... – maerics Commented Feb 19, 2013 at 16:59
  • I don't get the point of popnoodles' edit... – Denys Séguret Commented Feb 19, 2013 at 17:00
  • @dystroy OP had used characters that were not being displayed because they had not indented the code. Did you not pare the two versions before menting? – Popnoodles Commented Feb 19, 2013 at 17:01
 |  Show 2 more ments

3 Answers 3

Reset to default 8

Demo

var json = '[{"userId" : "123123", "password": "fafafa", "age": "21"}, {"userId" : "321321", "password" : "nana123", "age" : "34"}]';

var list = JSON.parse(json);
var output = {};

for(var i=0; i<list.length; i++)
{
    for(var key in list[i])
    {
        if(list[i].hasOwnProperty(key))
        {
            if(typeof output[key] == 'undefined')
            {
                output[key] = [];
            }
            output[key].push(list[i][key]);
        }
    }
}

document.write(JSON.stringify(output));

Outputs:

{"userId":["123123","321321"],"password":["fafafa","nana123"],"age":["21","34"]}

function mergeAttributes(arr) {
  return arr.reduce(function(memo, obj) { // For each object in the input array.
    Object.keys(obj).forEach(function(key) { // For each key in the object.
      if (!(key in memo)) { memo[key] = []; } // Create an array the first time.
      memo[key].push(obj[key]); // Add this property to the reduced object.
    });
    return memo;
  }, {});
}

var json = '[{"userId" : "123123", "password": "fafafa", "age": "21"}, {"userId" : "321321", "password" : "nana123", "age" : "34"}]';

mergeAttributes(JSON.parse(json));
// {
//   "userId": ["123123", "321321"],
//   "password": ["fafafa", "nana123"],
//   "age": ["21", "34"]
// }

Javascript's JSON.stringify will help you to convert any JSON pliant object model into a JSON string.

发布评论

评论列表(0)

  1. 暂无评论