我有一个数组对象和一个数组。该数组中的哪些值与数组对象中的值匹配需要更改状态(如 true)。
arrObj 输出应如下所示:
[
{ name: "Test1", status: true },
{ name: "Test2", status: false },
{ name: "Test3", status: true }
]
演示:https://stackblitz.com/edit/github-7plax5-mu3l6a?file=src%2Fapp%2Fapp.component.ts
let arrObj = [{
name: "Test1",
status: true
},
{
name: "Test2",
status: true
},
{
name: "Test3",
status: true
}
]
let arr = ["Test1", "Test3"];
for (k of arr) {
if (arrObj.name == k) {
arrObj.status = true;
}
}
console.log(arrObj);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
修改原始数组的最简单方法
<script> let arrObj = [{ name: "Test1", status: false }, { name: "Test2", status: false }, { name: "Test3", status: false } ] let arr = ["Test1", "Test3"]; </script>Typescript 版本 运行与我已经发布的脚本相同的脚本推断的类型