js如何根据对象相同的属性拼接数组
`
{
"data": [{
"id": 582,
"bigQuestionIndex": 0,
"paragraphIndex": 0,
"sentenceIndex": 0,
"type": "essay",
"sentenceWords": [
"",
"",
"Whatever",
"is",
"worth",
"doing",
"is",
"worth",
"doing",
"well",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 583,
"bigQuestionIndex": 0,
"paragraphIndex": 0,
"sentenceIndex": 1,
"type": "essay",
"sentenceWords": [
"Happiness",
"is",
"a",
"way",
"station",
"between",
"too",
"much",
"and",
"too",
"little",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 584,
"bigQuestionIndex": 0,
"paragraphIndex": 0,
"sentenceIndex": 2,
"type": "essay",
"sentenceWords": [
"In",
"love",
"folly",
"is",
"always",
"sweet",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 585,
"bigQuestionIndex": 0,
"paragraphIndex": 1,
"sentenceIndex": 0,
"type": "essay",
"sentenceWords": [
"",
"",
"Love",
"is",
"not",
"a",
"maybe",
"thing",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 586,
"bigQuestionIndex": 0,
"paragraphIndex": 1,
"sentenceIndex": 1,
"type": "essay",
"sentenceWords": [
"You",
"know",
"when",
"you",
"love",
"someone",
"!"
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 587,
"bigQuestionIndex": 0,
"paragraphIndex": 1,
"sentenceIndex": 2,
"type": "essay",
"sentenceWords": [
"Time",
"goes",
"by",
"so",
"fast",
",",
"people",
"go",
"in",
"and",
"out",
"of",
"your",
"life",
"?"
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 588,
"bigQuestionIndex": 0,
"paragraphIndex": 1,
"sentenceIndex": 3,
"type": "essay",
"sentenceWords": [
"You",
"must",
"never",
"miss",
"the",
"opportunity",
"to",
"tell",
"these",
"people",
"how",
"much",
"they",
"mean",
"to",
"you",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 589,
"bigQuestionIndex": 0,
"paragraphIndex": 2,
"sentenceIndex": 0,
"type": "essay",
"sentenceWords": [
"",
"",
"Love",
"is",
"not",
"a",
"maybe",
"thing",
","
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 590,
"bigQuestionIndex": 0,
"paragraphIndex": 2,
"sentenceIndex": 1,
"type": "essay",
"sentenceWords": [
"You",
"know",
"when",
"you",
"love",
"someone",
"!"
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 591,
"bigQuestionIndex": 0,
"paragraphIndex": 2,
"sentenceIndex": 2,
"type": "essay",
"sentenceWords": [
"Time",
"goes",
"by",
"so",
"fast",
",",
"people",
"go",
"in",
"and",
"out",
"of",
"your",
"life",
"……"
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 592,
"bigQuestionIndex": 0,
"paragraphIndex": 2,
"sentenceIndex": 3,
"type": "essay",
"sentenceWords": [
"You",
"must",
"never",
"miss",
"the",
"opportunity",
"to",
"tell",
"these",
"people",
"how",
"much",
"they",
"mean",
"to",
"you",
";"
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 593,
"bigQuestionIndex": 0,
"paragraphIndex": 100,
"sentenceIndex": 0,
"type": "topic",
"sentenceWords": [
"1.",
"Lily",
"was",
"so",
"___",
"looking",
"at",
"the",
"picture",
"that",
"she",
"forgot",
"the",
"time",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 594,
"bigQuestionIndex": 0,
"paragraphIndex": 101,
"sentenceIndex": 0,
"type": "option",
"sentenceWords": [
"[A].",
"carefully"
],
"passageId": 46,
"questionId": 593,
"version": 1
},
{
"id": 595,
"bigQuestionIndex": 0,
"paragraphIndex": 102,
"sentenceIndex": 0,
"type": "option",
"sentenceWords": [
"[B].",
"careful"
],
"passageId": 46,
"questionId": 593,
"version": 1
},
{
"id": 596,
"bigQuestionIndex": 0,
"paragraphIndex": 103,
"sentenceIndex": 0,
"type": "option",
"sentenceWords": [
"[C].",
"busily"
],
"passageId": 46,
"questionId": 593,
"version": 1
},
{
"id": 597,
"bigQuestionIndex": 0,
"paragraphIndex": 104,
"sentenceIndex": 0,
"type": "option",
"sentenceWords": [
"[D].",
"busy"
],
"passageId": 46,
"questionId": 593,
"version": 1
},
{
"id": 598,
"bigQuestionIndex": 0,
"paragraphIndex": 200,
"sentenceIndex": 1,
"type": "topic",
"sentenceWords": [
"2.",
"Putting",
"paper",
"cuts",
"on",
"the",
"windows",
"______",
"good",
"luck",
"."
],
"passageId": 46,
"questionId": null,
"version": 1
},
{
"id": 599,
"bigQuestionIndex": 0,
"paragraphIndex": 201,
"sentenceIndex": 1,
"type": "option",
"sentenceWords": [
"[A].",
"means"
],
"passageId": 46,
"questionId": 598,
"version": 1
},
{
"id": 600,
"bigQuestionIndex": 0,
"paragraphIndex": 202,
"sentenceIndex": 1,
"type": "option",
"sentenceWords": [
"[B].",
"meaning"
],
"passageId": 46,
"questionId": 598,
"version": 1
},
{
"id": 601,
"bigQuestionIndex": 0,
"paragraphIndex": 203,
"sentenceIndex": 1,
"type": "option",
"sentenceWords": [
"[C].",
"to",
"mean"
],
"passageId": 46,
"questionId": 598,
"version": 1
},
{
"id": 602,
"bigQuestionIndex": 0,
"paragraphIndex": 204,
"sentenceIndex": 1,
"type": "option",
"sentenceWords": [
"[D].",
"mean"
],
"passageId": 46,
"questionId": 598,
"version": 1
}
]
}
`
后台返回如上数据结构的数组对象
根据data中元素的 paragraphIndex
进行判断 paragraphIndex 认为是 相同段落的句子 然后把 sentenceWords 进行拼接 paragraphIndex 那么该sentenceWords 即为一个自然段
最好能返回这样的一个对象
`
{
"id": 582,"bigQuestionIndex": 0,
"paragraphIndex": 0,
"sentenceIndex": 0,
"type": "essay",
"sentenceWords": [
拼接完好的句子
],
"passageId": 46,
"questionId": null,
"version": 1
},
`
效果如下
答
function unique(arr) { let hashTable = {};
let newArr = [];
for(let i = 0, l = arr.length; i < l ; i++) {
let fullwords = arr[i].sentenceWords.reduce((str, currStr) => str += currStr + ' ', '')
if(!hashTable[arr[i].sentenceIndex]) {
hashTable[arr[i].sentenceIndex] = true;
arr[i].sentenceWords = [fullwords]
newArr.push(arr[i]);
} else {
newArr.forEach(item => {
if (item.sentenceIndex == arr[i].sentenceIndex) {
item.sentenceWords = item.sentenceWords.concat(fullwords)
}
})
}
}
return newArr;
}
let result = unique(test)
console.log(result)
function joinText (arr) { let paragraphIndexMap = {}
arr.map((item) => {
if(paragraphIndexMap[item.paragraphIndex]) {
paragraphIndexMap[item.paragraphIndex].sentenceWords.push(item.sentenceWords.join(' '))
} else {
item.sentenceWords = [item.sentenceWords.join(' ')]
paragraphIndexMap[item.paragraphIndex] = item
}
})
return Object.values(paragraphIndexMap)
}
以上是 js如何根据对象相同的属性拼接数组 的全部内容, 来源链接: utcz.com/a/19871.html