Map a key value to the other values in the data

浪子不回头ぞ 提交于 2020-01-25 03:10:07

问题


Suppose I have data coming as json in the below format

[
    {
        "clauseId": 1,
        "clauseName": "cover",
        "texts": [
            {
                "textId": 1,
                "text": "hello"
            }
        ]
    },
    {
        "clauseId": 3,
        "clauseName": "xyz",
        "texts": [
            {
                "textId": 3,
                "text": "hello Everyone"
            },
            {
                "textId": 4,
                "text": "Some data"
            }
        ]
    }
 {
        "clauseId": 2,
        "clauseName": "joining",
        "texts": [
            {
                "textId": 3,
                "text": "hello1"
            },
            {
                "textId": 4,
                "text": "hello2"
            }
        ]
    }
]

I have made a list from clause Name like

c=[joining,xyz]

I want to make a list where the text is coming like

d=[hello Everyone,Some data,hello1,hello2]

Please suggest something about it


回答1:


You can filter and then get desired fields:

let filters = ['hello Everyone','Some data','hello1','hello2'];
const result = arr.filter(f => 
    f.texts.some(s => filters.includes(s.text)))
                  .map(a => a.clauseName);

const arr = [
  {
      "clauseId": 1,
      "clauseName": "cover",
      "texts": [
          {
              "textId": 1,
              "text": "hello"
          }
      ]
  },
  {
      "clauseId": 3,
      "clauseName": "xyz",
      "texts": [
          {
              "textId": 3,
              "text": "hello Everyone"
          },
          {
              "textId": 4,
              "text": "Some data"
          }
      ]
  },
{
      "clauseId": 2,
      "clauseName": "joining",
      "texts": [
          {
              "textId": 3,
              "text": "hello1"
          },
          {
              "textId": 4,
              "text": "hello2"
          }
      ]
  }
]

let filters = ['hello Everyone','Some data','hello1','hello2'];
const result = arr.filter(f=> f.texts.some(s => filters.includes(s.text))).map(a=>a.clauseName);
console.log(result);

UPDATE 1:

If you want to filter by ['joining','xyz'];, then you can use filters array and check whether data is contained by includes method:

let filters = ['joining','xyz'];
const result = arr.filter(f => filters.includes(f.clauseName))
                  .flatMap(r => r.texts.map(t => t.text));
console.log(result);

const arr = [
  {
      "clauseId": 1,
      "clauseName": "cover",
      "texts": [
          {
              "textId": 1,
              "text": "hello"
          }
      ]
  },
  {
      "clauseId": 3,
      "clauseName": "xyz",
      "texts": [
          {
              "textId": 3,
              "text": "hello Everyone"
          },
          {
              "textId": 4,
              "text": "Some data"
          }
      ]
  },
{
      "clauseId": 2,
      "clauseName": "joining",
      "texts": [
          {
              "textId": 3,
              "text": "hello1"
          },
          {
              "textId": 4,
              "text": "hello2"
          }
      ]
  }
]

let filters = ['joining','xyz'];
const result = arr.filter(f => filters.includes(f.clauseName))
                  .sort((a,b) => a.clauseId - b.clauseId)
                  .flatMap(r => r.texts.map(t => t.text));
console.log(result);



回答2:


Try like this:

Working Demo

result = [];

constructor() {
  let texts:any[] = this.data.filter(item => this.c.includes(item.clauseName)).sort((a, b) => a.clauseId - b.clauseId).flatMap(x => x.texts);

  this.result = texts.map(x => x.text);
}


来源:https://stackoverflow.com/questions/59065977/map-a-key-value-to-the-other-values-in-the-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!