해당 속성 값이있는 속성 목록이 포함 된 개체가 있습니다.
일부 값 배열에는 제거하고 싶은 중복 값이 있습니다.
예
let attributes = [{
"attribute": "Size",
"values": ["S", " S", "M", "L", "L"]
},{
"attribute": "Shape",
"values": ["Square", "Round", "Square", " Square", " Square"]
}]
let filtered_attributes = attributes.forEach(el => {
el.map(a => a.values.trim())
.filter((item, pos, self) => { return self.indexOf(item) === pos })
.sort();
})
원하는 출력
[{
"attribute": "Size",
"values": ["L", "M", "S"]
},{
"attribute": "Shape",
"values": ["Round", "Square"]
}]
배열의 각 객체에 대해 고유 값 목록 (공백이 먼저 제거 된 값)을 반환하려면 어떻게해야합니까?
당신이 사용할 수있는 map
플러스 소품에 도착 destructuring, 당신은 할 수 있습니다 filter
당신의 가치는 거기에서 소품 trim
의 밍 각 값의 내부 filter
.
let attributes = [{
"attribute": "Size",
"values": ["S", " S", "M", "L", "L"]
},{
"attribute": "Shape",
"values": ["Square", "Round", "Square", " Square", " Square"]
}]
// usage example:
let unique = attributes.map(({ attribute, values }) => ({
attribute, values: values.filter((v, i, s) => s.indexOf(v.trim()) === i) // v, i, s = value, index, self
}));
console.log(unique)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다