Javascript - adding objects to array with matching id - Stack Overflow

I have an array categories that has the value id which I am trying to match with my array articlesIf a

I have an array categories that has the value id which I am trying to match with my array articles

If articles has a matching id with one of the categories I like would to bine them into one array. I am able to add these objects to the array but they are not matching based on id.

Here is an example of categories:

const categories = [{results: 
    { 
      "id": 28,
      "name": "Articles"
    }, { 
      "id": 76,
      "name": "Projects"
    }
    }]

Here is an example of articles:

const articles = [ 
{
 "title": "first article",
 "content": "lorem ipsum",
 "categoryId": 28
},
{
 "title": "second article",
 "content": "lorem ipsum",
 "categoryId": 28
},
{
 "title": "thirdarticle",
 "content": "lorem ipsum",
 "categoryId": 76
},
]

I am attempting to bine the two arrays into one. If possible I would like the articles array to be an array in that matching index. For example:

 const bined = [
      {
        "id": 28,
        "name": "Articles",
        "articles:: [
          { "title": "first article", "content": "lorem ipsum", "categoryId": 28 },
          { "title": "second article", "content": "lorem ipsum", "categoryId": 28 },
        ],
      },
    ];

I've attempted to do this by mapping through categories and using Object.assign. The output results seems to bine arrays but I believe they are all bined and not to match the id.

How am I able to achieve this?

const articles = [ 
    {
     "title": "first article",
     "content": "lorem ipsum",
     "categoryId": 28
    },
    {
     "title": "second article",
     "content": "lorem ipsum",
     "categoryId": 28
    },
    {
     "title": "thirdarticle",
     "content": "lorem ipsum",
     "categoryId": 76
    },
    ]
    

const categories = {results: [{ "id": 28, "name": "Articles"}, {"id": 76, "name": "Projects"}]}

let bined = categories.results.map((item, i ) => Object.assign(item, articles));

  console.log(bined)

I have an array categories that has the value id which I am trying to match with my array articles

If articles has a matching id with one of the categories I like would to bine them into one array. I am able to add these objects to the array but they are not matching based on id.

Here is an example of categories:

const categories = [{results: 
    { 
      "id": 28,
      "name": "Articles"
    }, { 
      "id": 76,
      "name": "Projects"
    }
    }]

Here is an example of articles:

const articles = [ 
{
 "title": "first article",
 "content": "lorem ipsum",
 "categoryId": 28
},
{
 "title": "second article",
 "content": "lorem ipsum",
 "categoryId": 28
},
{
 "title": "thirdarticle",
 "content": "lorem ipsum",
 "categoryId": 76
},
]

I am attempting to bine the two arrays into one. If possible I would like the articles array to be an array in that matching index. For example:

 const bined = [
      {
        "id": 28,
        "name": "Articles",
        "articles:: [
          { "title": "first article", "content": "lorem ipsum", "categoryId": 28 },
          { "title": "second article", "content": "lorem ipsum", "categoryId": 28 },
        ],
      },
    ];

I've attempted to do this by mapping through categories and using Object.assign. The output results seems to bine arrays but I believe they are all bined and not to match the id.

How am I able to achieve this?

const articles = [ 
    {
     "title": "first article",
     "content": "lorem ipsum",
     "categoryId": 28
    },
    {
     "title": "second article",
     "content": "lorem ipsum",
     "categoryId": 28
    },
    {
     "title": "thirdarticle",
     "content": "lorem ipsum",
     "categoryId": 76
    },
    ]
    

const categories = {results: [{ "id": 28, "name": "Articles"}, {"id": 76, "name": "Projects"}]}

let bined = categories.results.map((item, i ) => Object.assign(item, articles));

  console.log(bined)

Share Improve this question asked Jan 6, 2022 at 0:41 stepheniokstepheniok 4058 silver badges22 bronze badges 3
  • You're not looking for matching IDs anywhere. – Barmar Commented Jan 6, 2022 at 0:45
  • Hint: use the find() method to find an object with a given property value. – Barmar Commented Jan 6, 2022 at 0:45
  • Your initial example of categories is not valid JavaScript. It's supposd to be an object containing an array, but it's an array containing an invalid object. – Barmar Commented Jan 6, 2022 at 0:47
Add a ment  | 

1 Answer 1

Reset to default 7

Here's one way to get there using your code but filtering matching articles and using the spread operator

let bined = categories.results.map(item => ({ ...item,
  articles: articles.filter(f => f.categoryId == item.id)
}));

const articles = [{
    "title": "first article",
    "content": "lorem ipsum",
    "categoryId": 28
  },
  {
    "title": "second article",
    "content": "lorem ipsum",
    "categoryId": 28
  },
  {
    "title": "thirdarticle",
    "content": "lorem ipsum",
    "categoryId": 76
  },
]


const categories = {
  results: [{
    "id": 28,
    "name": "Articles"
  }, {
    "id": 76,
    "name": "Projects"
  }]
}

let bined = categories.results.map(item => ({ ...item,
  articles: articles.filter(f => f.categoryId == item.id)
}));

console.log(bined)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745465153a4628885.html

相关推荐

  • Javascript - adding objects to array with matching id - Stack Overflow

    I have an array categories that has the value id which I am trying to match with my array articlesIf a

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信