所以我有一个JSON对象[{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}]
我基本上需要删除键,因此输出在Javascript中看起来像[["val1","val2"],["val1","val2"]]
。
没有迭代遍历数组然后遍历所有属性并映射到新的JSON对象有什么办法可以从对象中删除键,并将值转换为数组中的列表?
请不要使用字符串拼接/正则表达式。
感谢。
答案 0 :(得分:4)
使用ES2015(ES6)
{ (data : CMPedometerData?, error: NSError?) -> Void in
DispatchQueue.main.async(){
见
答案 1 :(得分:0)
如果您仍需要旧的浏览器支持 - 前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);
答案 2 :(得分:0)
普通ES5,Array#map
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; }
答案 3 :(得分:0)
您需要遍历对象并创建新数组。
for (var i = 0; i < yourobject.length; i++) {
var myArray = [];
for (var key in yourobject) {
myArray.push(yourobject[key]
}
console.log(myArray)
}
答案 4 :(得分:0)
这应该这样做:
const arr = [{"key1":"val1","key2":"val2"},{"key1":"val1","key2":"val2"}];
const newArr = arr.map(obj => Object.values(obj));