简体   繁体   English

如何从一个 json object 获得两个 arrays

[英]How to get two arrays from one json object

i have this:我有这个:

    var keys = ['foo', 'bar', 'baz'];
    var values = [11, 22, 33]

    var result = {};
    keys.forEach((key, i) => result[key] = values[i]);
    console.log(result);

which is the right way to push the keys ans the values back to separated arrays from json result?这是将键和值从 json 结果推回分离的 arrays 的正确方法?

    result = {"foo:11,"bar":22,"baz":33}
    keys = ['foo', 'bar', 'baz'];
    values = [11, 22, 33]

basically, for Objects in javascript you can use 2 useful functions called Object.keys and Object.values , you can check the documentation to see if any other function can work for you. basically, for Objects in javascript you can use 2 useful functions called Object.keys and Object.values , you can check the documentation to see if any other function can work for you.

check the Methods section检查方法部分

 const result = { "foo": 11, "bar": 22, "baz": 33 } const keys = Object.keys(result); const values = Object.values(result); console.log("here you have the keys") console.log(keys) console.log("here you have the values") console.log(values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM