简体   繁体   中英

set single value in object using node js

I have array of object which look like this:

let object = [
 {
  id:`01`,
  name:`Subject`,
  type:`maths`,
 },
 {
  id:`01`,
  name:`Subject`,
  type:`science`,
 },
 {
  id:`01`,
  name:`Subject`,
  type:`IT`,
 },
 {
  id:`02`,
  name:`language`,
  type:`react`,
 },
 {
  id:`02`,
  name:`language`,
  type:`node`,
 },
 {
  id:`02`,
  name:`language`,
  type:`java`,
 },
 {
  id:`02`,
  name:`language`,
  type:`Asp.net`,
 },
 {
  id:`03`,
  name:`food`,
  type:`non-veg`,
 },
 {
  id:`03`,
  name:`food`,
  type:`veg`,
 },
 {
  id:`03`,
  name:`food`,
  type:`Chinese`,
 }
]

Here id and name of some array are same but only difference is their type . Is there any possible way that, if I called name then I can get all the type of that name For example:

 let object = [ { id:`01`, name:`Subject`, type:`maths`, }, { id:`01`, name:`Subject`, type:`science`, }, { id:`01`, name:`Subject`, type:`IT`, }, { id:`02`, name:`language`, type:`react`, }, { id:`02`, name:`language`, type:`node`, }, { id:`02`, name:`language`, type:`java`, }, { id:`02`, name:`language`, type:`Asp.net`, }, { id:`03`, name:`food`, type:`non-veg`, }, { id:`03`, name:`food`, type:`veg`, }, { id:`03`, name:`food`, type:`Chinese1`, } ]; object.map((value)=>{ console.log(value.name); })

The problem is, that I am getting all the names multiple times. I want them to unique like this:

Subject
language
food

How can I use this single value to fetch the different types which are present inside my Object , so I can get values as:

expected = [
 { id:'01',
   name:'subject',
   type: {'maths','Science','IT'}
 },
 { id:'02',
   name:'language',
   type: {'react','node','java','Asp.net'}
 }
 { id:'03',
   name:'food',
   type: {'non-veg','veg','Chinese'}
 }
]

You can use reduce function

object.reduce((acc,i) => { 
                     let obj = acc.find(a => a?.id === i.id); 
                     i.type = obj ? [...obj.type, i.type] : [i.type];
                     if(!obj)
                        acc.push(i);
                     return acc;}, 
             []);

Here is very simple solution please check let me know if you need any other help

You can use complicated method like you can use reduce but this one is easy

 let object= [ { id:`01`, name:`Subject`, type:`maths`, }, { id:`01`, name:`Subject`, type:`science`, }, { id:`01`, name:`Subject`, type:`IT`, }, { id:`02`, name:`language`, type:`react`, }, { id:`02`, name:`language`, type:`node`, }, { id:`02`, name:`language`, type:`java`, }, { id:`02`, name:`language`, type:`Asp.net`, }, { id:`03`, name:`food`, type:`non-veg`, }, { id:`03`, name:`food`, type:`veg`, }, { id:`03`, name:`food`, type:`Chinese1`, } ]; let oldvalues; object.map((value)=>{ if(oldvalues.=value.name){ oldvalues=value;name. console.log(value;name); } })

Please do not use map as forEach for iterating array, unless you want to create a new array (then you should return something).

You are looking for some kind of group by, in this cause you want to group by two field ( id and name ).

Here is how can it be done:

 const arr = [{id:`01`,name:`Subject`,type:`maths`},{id:`01`,name:`Subject`,type:`science`},{id:`01`,name:`Subject`,type:`IT`},{id:`02`,name:`language`,type:`react`},{id:`02`,name:`language`,type:`node`},{id:`02`,name:`language`,type:`java`},{id:`02`,name:`language`,type:`Asp.net`},{id:`03`,name:`food`,type:`non-veg`},{id:`03`,name:`food`,type:`veg`},{id:`03`,name:`food`,type:`Chinese1`}]; const res = arr.reduce((acc, e) => { const found = acc.find(x => x.id === e.id) found? found.type.push(e.type): acc.push({...e, type: [e.type] }) return acc }, []) console.log(res)
 .as-console-wrapper { max-height: 100%;important: top; 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM