let segments = [
{
"value": [2,1,5,7],
"text": {
"hi": "hello"
},
"diff": 1
},
{
"value": [5,6],
"text": {
"hi": "bye"
},
"diff": 2
}
]
let segments = [
{
"value": 2,
"text": {
"hi": "hello"
},
"diff": 1
},
{
"value": 1,
"text": {
"hi": "hello"
},
"diff": 1
},
{
"value": 5,
"text": {
"hi": "hello"
},
"diff": 1
},
{
"value": 7,
"text": {
"hi": "hello"
},
"diff": 1
},
{
"value": 5,
"text": {
"hi": "bye"
},
"diff": 2
},
{
"value": 6,
"text": {
"hi": "bye"
},
"diff": 2
}
]
let result = segments.reduce(foo, [])
function foo (acc, segment){
segment.value.forEach(val => {
let newSegment = Object.assign({}, segment)
newSegment.value = val
acc.push(newSegment)
})
return acc
}
let result = segments.reduce((a,s)=>a.concat(s.value.map(v=>Object.assign({},s,{value:v}))),[])
map()
это много → многоreduce()
это много → одинvalues
получить массив объектов с полями родителя (это map()
).reduce()
).let result = segments
.map(e => e.value.map(
v => ({
value: v,
text : { hi: e.text.hi},
diff : e.diff
})
))
.reduce((prev, curr) => prev.concat(curr), [])
;