将键值对的JSON数组转换为Javascript中的字符串的JSON数组

时间:2022-08-23 08:19:26

so i have a JSON Object [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]

所以我有一个JSON对象[{“key1”:“val1”、“key2”:“val2”},{“key1”:“val1”、“key2”:“val2”}]

and i essentially need to remove the keys so the output looks like [["val1","val2"],["val1","val2"]] in Javascript.

本质上,我需要删除键,以便在Javascript中输出看起来像[[["val1","val2"],["val1","val2"]。

short of iterating through the array and then iterating through all the properties and mapping to a new JSON object is there any way i can remove the keys from the object, and turn the values in to a list in an array?

除了遍历数组然后遍历所有属性并映射到一个新的JSON对象之外,我是否可以从对象中删除键,并将值转换为数组中的列表?

please no string splicing/ regex.

请不要使用字符串拼接/正则表达式。

Thanks.

谢谢。

5 个解决方案

#1


4  

Using ES2015 (ES6)

使用ES2015(ES6)

const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]
arr.map(o => Object.values(o));

See

看到

#2


0  

If you still need old browser support--pre ES6

如果您仍然需要旧的浏览器支持——pre ES6

var arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}];

arr = arr.map(function(o){
   var a = [];
   for(var i in o){
      a.push(o[i])
   }
   return a;
});
console.log(arr);

#3


0  

Plain ES5 with Array#map

平原ES5数组#地图

var array = [{ key1: "val1", key2: "val2" },{ key1: "val1", key2: "val2" }],
    mapped = array.map(function (o) {
        return Object.keys(o).map(function (k) {
            return o[k];
        });
    });

console.log(mapped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#4


0  

You need to loop over the object and make new arrays.

您需要对对象进行循环并创建新的数组。

for (var i = 0; i < yourobject.length; i++) {
    var myArray = [];
    for (var key in yourobject) {
         myArray.push(yourobject[key]
    }
    console.log(myArray)
}

#5


0  

This should do it:

这应该这样做:

const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}];

const newArr = arr.map(obj => Object.values(obj));

#1


4  

Using ES2015 (ES6)

使用ES2015(ES6)

const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]
arr.map(o => Object.values(o));

See

看到

#2


0  

If you still need old browser support--pre ES6

如果您仍然需要旧的浏览器支持——pre ES6

var arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}];

arr = arr.map(function(o){
   var a = [];
   for(var i in o){
      a.push(o[i])
   }
   return a;
});
console.log(arr);

#3


0  

Plain ES5 with Array#map

平原ES5数组#地图

var array = [{ key1: "val1", key2: "val2" },{ key1: "val1", key2: "val2" }],
    mapped = array.map(function (o) {
        return Object.keys(o).map(function (k) {
            return o[k];
        });
    });

console.log(mapped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

#4


0  

You need to loop over the object and make new arrays.

您需要对对象进行循环并创建新的数组。

for (var i = 0; i < yourobject.length; i++) {
    var myArray = [];
    for (var key in yourobject) {
         myArray.push(yourobject[key]
    }
    console.log(myArray)
}

#5


0  

This should do it:

这应该这样做:

const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}];

const newArr = arr.map(obj => Object.values(obj));