问答

js如何根据对象相同的某个属性拼接数组

作者:admin 2021-07-30 我要评论

` { "data": [ { "id": 582, "bigQuestionIndex": 0, "paragraphIndex": 0, "sentenceIndex": 0, "type": "essay", "sentenceWords": [ "", "", "Whatever", "is...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

`
{

"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)
  }

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • js如何根据对象相同的某个属性拼接数组

    js如何根据对象相同的某个属性拼接数组

  • mysql主主复制如何能让A 数据库 出问题

    mysql主主复制如何能让A 数据库 出问题

  • 代码如何同时兼容React-native和Node环

    代码如何同时兼容React-native和Node环

  • Map.forEach如何优雅的跳出循环?

    Map.forEach如何优雅的跳出循环?

腾讯云代理商
海外云服务器