array
[{
"key":"Passed",
"values":[[date1,time1],[date2,time2]]
},{
"key":"Failed",
"values":[[date3,time3],[date4,time4]]
}]
Say i want to copy key:failed and values [date3,time3] to a new array, how do i do it?
假设我想复制密钥:失败并将值[date3,time3]复制到一个新数组,我该怎么办?
2 个解决方案
#1
0
First you will want to identify the object in the array by it's key. Lets assume you array is stored in a variable called mylist
首先,您需要通过它的键来识别数组中的对象。让我们假设您的数组存储在一个名为mylist的变量中
var error = mylist.find(function(item){ return item.key === 'Failed'; });
now assuming you have a new array:
现在假设你有一个新的数组:
var failed = []; // your new array
failed.push({
key: error.key,
values: error.values[0] // assuming you only want the first value
});
There may be a better application for this though. If you could provide more context around what you want achieve.
尽管如此,可能有更好的应用。如果您可以提供更多关于您想要实现的内容。
EDIT:
If you are looking to grab every failed result from the array and group them into a subset you may want user the reduce method to do the following.
如果您希望从阵列中获取每个失败的结果并将它们分组到子集中,您可能希望用户使用reduce方法执行以下操作。
var failed = mylist.reduce(function(mem, item) {
if(item.key === 'Failed'){
var transformed = {
key: error.key,
values: error.values[0]
};
mem.push(transform)
}
return mem;
}, []);
#2
0
Using just JavaScript
仅使用JavaScript
var newArray = [];
for(var i=0; i<=array.length; i++){
if (array[i]['key'] === "Failed"){
newArray.push(array[i]);
}
}
newArray
contains all the failed items.
newArray包含所有失败的项目。
#1
0
First you will want to identify the object in the array by it's key. Lets assume you array is stored in a variable called mylist
首先,您需要通过它的键来识别数组中的对象。让我们假设您的数组存储在一个名为mylist的变量中
var error = mylist.find(function(item){ return item.key === 'Failed'; });
now assuming you have a new array:
现在假设你有一个新的数组:
var failed = []; // your new array
failed.push({
key: error.key,
values: error.values[0] // assuming you only want the first value
});
There may be a better application for this though. If you could provide more context around what you want achieve.
尽管如此,可能有更好的应用。如果您可以提供更多关于您想要实现的内容。
EDIT:
If you are looking to grab every failed result from the array and group them into a subset you may want user the reduce method to do the following.
如果您希望从阵列中获取每个失败的结果并将它们分组到子集中,您可能希望用户使用reduce方法执行以下操作。
var failed = mylist.reduce(function(mem, item) {
if(item.key === 'Failed'){
var transformed = {
key: error.key,
values: error.values[0]
};
mem.push(transform)
}
return mem;
}, []);
#2
0
Using just JavaScript
仅使用JavaScript
var newArray = [];
for(var i=0; i<=array.length; i++){
if (array[i]['key'] === "Failed"){
newArray.push(array[i]);
}
}
newArray
contains all the failed items.
newArray包含所有失败的项目。