我试图为数组中的每个对象设置一个布尔属性.
import { Component, OnInit } from "@angular/core";
import * as _ from "lodash";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
title = "CodeSandbox";
colors: {hex: string, used: boolean}[] = [
{hex: "#ff0000", used: true},
{hex: "#00ff00", used: true},
{hex: "#0000ff", used: true}
]
ngOnInit() {
_.forEach(this.colors, c => c.used = false);
console.log(this.colors);
}
}
显然只有第一个物体会受到影响.这是输出.
0: Object
hex: "#ff0000"
used: false
1: Object
hex: "#00ff00"
used: true
2: Object
hex: "#0000ff"
used: true
这就是Sandbox :https://codesandbox.io/s/strange-water-p19ihj?file=/src/app/app.component.ts
它与_.map和原生forEach一起使用.我是不是漏掉了什么,还是有什么问题?