JavaScript Array of Arrays: 想要获取值为 'PROCESSED' 的第一行
JavaScript Array of Arrays: want to get the first row where the value is 'PROCESSED'
我如何 return 只有第一个通过条件的值,其中值为以下数组中的“PROCESSED
”?
[
[ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ],
[ '21/01/2021 11:09:07', '222', 'PROCESSED' ],
[ '21/01/2021 11:08:57', '333', 'PROCESSED' ],
[ '21/01/2021 11:08:57', '444', 'PROCESSED' ],
[ '21/01/2021 11:07:25', '555', 'PROCESSED' ],
]
我想return原始数组中的整行和索引。
试试这个:
const data = [
["21/01/2021 11:11:57", "111", "IN PROGRESS"],
["21/01/2021 11:09:07", "222", "PROCESSED"],
["21/01/2021 11:08:57", "333", "PROCESSED"],
["21/01/2021 11:08:57", "444", "PROCESSED"],
["21/01/2021 11:07:25", "555", "PROCESSED"],
];
const result = data.filter((item) => item[2] === "PROCESSED")[0];
console.log(result);
您可以使用 Array#findIndex
const arr = [[ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ], [ '21/01/2021 11:09:07', '222', 'PROCESSED' ], [ '21/01/2021 11:08:57', '333', 'PROCESSED' ], [ '21/01/2021 11:08:57', '444', 'PROCESSED' ], [ '21/01/2021 11:07:25', '555', 'PROCESSED' ], ];
let ind = arr.findIndex(a=> a[2] == 'PROCESSED');
console.log(arr[ind],ind)
I want to return the whole row and the index in the original array.
根据需要索引,可以使用.findIndex() with a combination of destructuring assignment 从数组中提取第二个索引([,,status]
) 处的字符串。然后可以对第二个索引为 'PROCESSED'
的第一项 return true 来获取索引。一旦你有了索引,你就可以抓取行(即:索引处的内部数组):
const arr = [ [ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ], [ '21/01/2021 11:09:07', '222', 'PROCESSED' ], [ '21/01/2021 11:08:57', '333', 'PROCESSED' ], [ '21/01/2021 11:08:57', '444', 'PROCESSED' ], [ '21/01/2021 11:07:25', '555', 'PROCESSED' ], ];
const idx = arr.findIndex(([,,status]) => status === "PROCESSED");
const row = arr[idx];
console.log(idx, row);
我如何 return 只有第一个通过条件的值,其中值为以下数组中的“PROCESSED
”?
[
[ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ],
[ '21/01/2021 11:09:07', '222', 'PROCESSED' ],
[ '21/01/2021 11:08:57', '333', 'PROCESSED' ],
[ '21/01/2021 11:08:57', '444', 'PROCESSED' ],
[ '21/01/2021 11:07:25', '555', 'PROCESSED' ],
]
我想return原始数组中的整行和索引。
试试这个:
const data = [
["21/01/2021 11:11:57", "111", "IN PROGRESS"],
["21/01/2021 11:09:07", "222", "PROCESSED"],
["21/01/2021 11:08:57", "333", "PROCESSED"],
["21/01/2021 11:08:57", "444", "PROCESSED"],
["21/01/2021 11:07:25", "555", "PROCESSED"],
];
const result = data.filter((item) => item[2] === "PROCESSED")[0];
console.log(result);
您可以使用 Array#findIndex
const arr = [[ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ], [ '21/01/2021 11:09:07', '222', 'PROCESSED' ], [ '21/01/2021 11:08:57', '333', 'PROCESSED' ], [ '21/01/2021 11:08:57', '444', 'PROCESSED' ], [ '21/01/2021 11:07:25', '555', 'PROCESSED' ], ];
let ind = arr.findIndex(a=> a[2] == 'PROCESSED');
console.log(arr[ind],ind)
I want to return the whole row and the index in the original array.
根据需要索引,可以使用.findIndex() with a combination of destructuring assignment 从数组中提取第二个索引([,,status]
) 处的字符串。然后可以对第二个索引为 'PROCESSED'
的第一项 return true 来获取索引。一旦你有了索引,你就可以抓取行(即:索引处的内部数组):
const arr = [ [ '21/01/2021 11:11:57', '111', 'IN PROGRESS' ], [ '21/01/2021 11:09:07', '222', 'PROCESSED' ], [ '21/01/2021 11:08:57', '333', 'PROCESSED' ], [ '21/01/2021 11:08:57', '444', 'PROCESSED' ], [ '21/01/2021 11:07:25', '555', 'PROCESSED' ], ];
const idx = arr.findIndex(([,,status]) => status === "PROCESSED");
const row = arr[idx];
console.log(idx, row);