How to change array object property value javascript
P粉618358260
2023-09-03 14:27:32
<p>I have an array object and an array. Which values in this array match values in the array object require a change of state (such as true). </p>
<p><code>arrObj</code> The output should look like this: </p>
<pre class="brush:php;toolbar:false;">[
{ name: "Test1", status: true },
{ name: "Test2", status: false },
{ name: "Test3", status: true }
]</pre>
<p>Demo: https://stackblitz.com/edit/github-7plax5-mu3l6a?file=src/app/app.component.ts</p>
<p>
<pre class="snippet-code-js lang-js prettyprint-override"><code>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);</code></pre>
</p>
The simplest way to modify the original array
Typescript Version Types inferred by running the same script as the one I have posted