1

I have array structure as shown in below

[
  {
    "id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
    "refId": "1",
    "reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
    "list": [
      {
        "text": "<p>Annot 1</p>",
        "id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
      },
      {
        "text": "<p>Annot 1</p>",
        "id": "e1486639-aa46-4513-992b-17d8d66f06c8"
      }
    ]
  },
  {
    "id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
    "refId": "2",
    "reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
    "list": [
      {
        "text": "<p>Annot 2</p>",
        "id": "fc430895-912a-4e4b-884c-f08600e06ec8"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "090737d5-48ac-4e3f-973d-9e1de206252b"
      },
      {
        "text": "<p>Annot 2</p>",
        "id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
      }
    ]
  },
  {
    "id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
    "refId": "3",
    "reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
    "list": [
      {
        "text": "<p>Annot 3</p>",
        "id": "f9082af8-3c2f-477b-b113-a96f320415d1"
      },
      {
        "text": "<p>Annot 3</p>",
        "id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
      }
    ]
  }
]

Expected result is

[
  {
    "id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
    "refId": "1",
    "reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
    "list": [
      {
        "text": "<p>Annot 1</p>",
        "id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
      }
    ]
  },
  {
    "id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
    "refId": "2",
    "reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
    "list": [
      {
        "text": "<p>Annot 2</p>",
        "id": "fc430895-912a-4e4b-884c-f08600e06ec8"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
      }
    ]
  },
  {
    "id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
    "refId": "3",
    "reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
    "list": [
      {
        "text": "<p>Annot 3</p>",
        "id": "f9082af8-3c2f-477b-b113-a96f320415d1"
      }
    ]
  }
]

I have tried this solution but didnot get the expected result.

const finalListmergedArray = [
  {
    "id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
    "refId": "1",
    "reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
    "list": [
      {
        "text": "<p>Annot 1</p>",
        "id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
      },
      {
        "text": "<p>Annot 1</p>",
        "id": "e1486639-aa46-4513-992b-17d8d66f06c8"
      }
    ]
  },
  {
    "id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
    "refId": "2",
    "reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
    "list": [
      {
        "text": "<p>Annot 2</p>",
        "id": "fc430895-912a-4e4b-884c-f08600e06ec8"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "090737d5-48ac-4e3f-973d-9e1de206252b"
      },
      {
        "text": "<p>Annot 2</p>",
        "id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
      }
    ]
  },
  {
    "id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
    "refId": "3",
    "reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
    "list": [
      {
        "text": "<p>Annot 3</p>",
        "id": "f9082af8-3c2f-477b-b113-a96f320415d1"
      },
      {
        "text": "<p>Annot 3</p>",
        "id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
      }
    ]
  }
]
    // Loop through array values
    for (let i = 0; i < finalListmergedArray.length; i++) {
      for (
        let j = 0;
        j < finalListmergedArray[i]["list"].length;
        j++
      ) {
        var result = [
          ...new Map(
            finalListmergedArray[i]["list"].map((o) => [
              JSON.stringify(o),
              o,
            ])
          ).values(),
        ];
        finalListmergedArray[i]["list"] = result;
      }
    }
    console.log(finalListmergedArray)

Any help would be appreciated. Thanks, in advance.

3 Answers 3

1

you want to remove text duplicates values from list attribute. To be clear, those objects are actually unique because each one has an unique id.

An approach to solve this is to create a Map to store unique text values for each object. Then you filter your list, validate if text is included at your Map. If it's included you return false, otherwise you add text to your Map and return true:

// data here is your array
const filteredData = data.map(item => {
  const includedText = new Map()
  const list = item.list.filter(content => {
    if (includedText.has(content.text)) return false
    includedText.set(content.text)
    return true
  })

  return { ...item, list }
})

console.log(filteredData)
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming you want to merge with text in the list, you can use a list to store the processed elements. Below is the code snipped for the same.

const input = [
  {
    "id": "cbdfc96a-c788-45cd-bdd1-7d573cc8c474",
    "refId": "1",
    "reference": "<p>1646 Journal of Investigative Dermatology ( 2017 ) , Volume 137</p>",
    "list": [
      {
        "text": "<p>Annot 1</p>",
        "id": "e9c207ae-3d08-4eb2-94d1-b6ae713862d3"
      },
      {
        "text": "<p>Annot 1</p>",
        "id": "e1486639-aa46-4513-992b-17d8d66f06c8"
      }
    ]
  },
  {
    "id": "90b187ab-26ef-45c0-a6a0-bf7dcfde660f",
    "refId": "2",
    "reference": "<p>ZK Jabbar - Lopez et al . Evaluation of Biologic Therapy Options for Psoriasis</p>",
    "list": [
      {
        "text": "<p>Annot 2</p>",
        "id": "fc430895-912a-4e4b-884c-f08600e06ec8"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "a1296b1f-530e-4e1b-b2d0-f97bba192a1a"
      },
      {
        "text": "<p>Annot 2 3</p>",
        "id": "090737d5-48ac-4e3f-973d-9e1de206252b"
      },
      {
        "text": "<p>Annot 2</p>",
        "id": "6d5f89be-305e-4751-a7ca-75d9b0f2057e"
      }
    ]
  },
  {
    "id": "e8c3a958-7fa4-40e1-a83a-44c85ebb05a7",
    "refId": "3",
    "reference": "<p>Table 2. Relative treatment rankings ( outcomes at 12 to 16 wk )</p>",
    "list": [
      {
        "text": "<p>Annot 3</p>",
        "id": "f9082af8-3c2f-477b-b113-a96f320415d1"
      },
      {
        "text": "<p>Annot 3</p>",
        "id": "2d7e65b8-e516-40bf-8957-c0fb4b819aa9"
      }
    ]
  }
]
//Solution....
input.forEach(function(i) {
    let processed = [];
    const newList = [];
    i.list.forEach(function(l) {
      if( processed.indexOf(l.text) < 0 ) { //id not in list
        newList.push(l);
        processed.push(l.text);
      }
    });
    i.list = newList;
});


console.log(input);

Comments

0

To solve with Rambda:

const expected = R.map(({list, ...rest}) => ({
  ...rest,
  list: R.uniqWith(R.eqProps('text'))(list)
}))(input)

See sandbox for your reference: https://ramdajs.com/repl/?v=0.27.0#?const%20input%20%3D%20%5B%0A%20%20%7B%0A%20%20%20%20%22id%22%3A%20%22cbdfc96a-c788-45cd-bdd1-7d573cc8c474%22%2C%0A%20%20%20%20%22refId%22%3A%20%221%22%2C%0A%20%20%20%20%22reference%22%3A%20%22%3Cp%3E1646%20Journal%20of%20Investigative%20Dermatology%20%28%202017%20%29%20%2C%20Volume%20137%3C%2Fp%3E%22%2C%0A%20%20%20%20%22list%22%3A%20%5B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%201%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22e9c207ae-3d08-4eb2-94d1-b6ae713862d3%22%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%201%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22e1486639-aa46-4513-992b-17d8d66f06c8%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%20%20%7D%2C%0A%20%20%7B%0A%20%20%20%20%22id%22%3A%20%2290b187ab-26ef-45c0-a6a0-bf7dcfde660f%22%2C%0A%20%20%20%20%22refId%22%3A%20%222%22%2C%0A%20%20%20%20%22reference%22%3A%20%22%3Cp%3EZK%20Jabbar%20-%20Lopez%20et%20al%20.%20Evaluation%20of%20Biologic%20Therapy%20Options%20for%20Psoriasis%3C%2Fp%3E%22%2C%0A%20%20%20%20%22list%22%3A%20%5B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%202%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22fc430895-912a-4e4b-884c-f08600e06ec8%22%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%202%203%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22a1296b1f-530e-4e1b-b2d0-f97bba192a1a%22%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%202%203%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22090737d5-48ac-4e3f-973d-9e1de206252b%22%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%202%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%226d5f89be-305e-4751-a7ca-75d9b0f2057e%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%20%20%7D%2C%0A%20%20%7B%0A%20%20%20%20%22id%22%3A%20%22e8c3a958-7fa4-40e1-a83a-44c85ebb05a7%22%2C%0A%20%20%20%20%22refId%22%3A%20%223%22%2C%0A%20%20%20%20%22reference%22%3A%20%22%3Cp%3ETable%202.%20Relative%20treatment%20rankings%20%28%20outcomes%20at%2012%20to%2016%20wk%20%29%3C%2Fp%3E%22%2C%0A%20%20%20%20%22list%22%3A%20%5B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%203%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22f9082af8-3c2f-477b-b113-a96f320415d1%22%0A%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22text%22%3A%20%22%3Cp%3EAnnot%203%3C%2Fp%3E%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%222d7e65b8-e516-40bf-8957-c0fb4b819aa9%22%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%20%20%7D%0A%5D%0A%0Aconst%20expected%20%3D%20R.map%28%28%20%7Blist%2C%20...rest%7D%29%20%3D%3E%20%28%7B%0A%20%20%20%20...rest%2C%0A%20%20%20%20list%3A%20R.uniqWith%28R.eqProps%28%27text%27%29%29%28list%29%0A%7D%29%29%28input%29%0A%0Aexpected%20%2F%2F%0A%0A

1 Comment

Cant we achieve this without any npm packege?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.