我正在从mysql中检索数据,并希望将localStorage中的每一行数据保存为数组.以下是我在javascript端收到的嵌套数组.
arr = [{image_id: "80", imagename: "Image1",firstx: "267", firsty: "403"},
{image_id: "80", imagename: "Image1",firstx: "320", firsty: "470"},
{image_id: "80", imagename: "Image2",firstx: "126", firsty: "237"}
]
Run Code Online (Sandbox Code Playgroud)
从这里我想删除image_id,imagename,firstx和firsty并返回一个数组的结果,并且只包含每个数组的值.期望的输出是
newarr =[[80,Image1,267,403],[80,Image1,320,470],[80,Image2,126,237]]
Run Code Online (Sandbox Code Playgroud)
我做了以下事情:
var newarr = [];
for (var i = 0, l = arr.length; i < l; i++) {
var keys = Object.keys(arr[i]);
for (var j = 0, k = keys.length; j < k; j++) {
newarr.push(arr[i][keys[j]]);
}
}
console.log(newarr)
Run Code Online (Sandbox Code Playgroud)
这将返回作为数组的每个元素.结果数组将作为嵌套数组推送到localStorage.
我猜你想要一个数组而不是一个对象 - map
通过Object.values
:
const arr = [{image_id: "80", imagename: "Image1",firstx: "267", firsty: "403"},
{image_id: "80", imagename: "Image1",firstx: "320", firsty: "470"},
{image_id: "80", imagename: "Image2",firstx: "126", firsty: "237"}
];
console.log(arr.map(Object.values));
Run Code Online (Sandbox Code Playgroud)