0

I have some array of objects value and I need to check some key value pair and update the objects accordingly using Javascript. I am explaining my code below.\

let arr =[
  {
    'id': 11,
    'name':'Raj',
    "code":0
  },
  {
    'id': 11,
    'name':'Raj',
    'code': 1
  },
  {
    'id':32,
    'name':'Krishna',
    'code':1
  },
  {
    'id':32,
    'name':'fff',
    'code':0
  },
  {
    'id':43,
    'name':'FAQ',
    'code':0
  }
]

let arr1 = arr.map((item,i) => {
  const found = arr.some(el => el.code === 1);
  if(found) {
    
  }
}) 

Here I have array of objects and I need if any record has code=1 as per id then all record's code=1 belongs to same id. My expected output will be like below.

let arr =[
  {
    'id': 11,
    'name':'Raj',
    "code":1
  },
  {
    'id': 11,
    'name':'Raj',
    'code': 1
  },
  {
    'id':32,
    'name':'Krishna',
    'code':1
  },
  {
    'id':32,
    'name':'fff',
    'code':1
  },
  {
    'id':43,
    'name':'FAQ',
    'code':0
  }
]

Here for id=11 there is one record which has code=1 so all records belongs to id=11 the code value will be 1;

3
  • 2
    I am not getting what is your question here? Commented Jun 22, 2020 at 10:52
  • First it will check for same id is there any code==1 if present it will update code=1 for that id. Commented Jun 22, 2020 at 10:54
  • There is difference in code key value pair. Commented Jun 22, 2020 at 10:55

6 Answers 6

1

Since you have accepted it as an answer. Your question is wrongly framed. My solution just check if the id has some code value apart from 0 then it would be applicable to all of the objects of that id.

var arr =[ { 'id': 11, 'name':'Raj', "code":0 }, { 'id': 11, 'name':'Raj', 'code': 1 }, { 'id':32, 'name':'Krishna', 'code':1 }, { 'id':32, 'name':'fff', 'code':0 }, { 'id':43, 'name':'FAQ', 'code':0 }];

var result = arr.map((k,i,self)=>({...k, code:self.find(p=>p.id==k.id && p.code != 0)?.code || k.code}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

I think what you want is to first filter on the list again to find if there are any items with the same id that have code === 1.

That can be done in multiple ways. Below is one example

let arr = [{
    'id': 11,
    'name': 'Raj',
    "code": 0
  },
  {
    'id': 11,
    'name': 'Raj',
    'code': 1
  },
  {
    'id': 32,
    'name': 'Krishna',
    'code': 1
  },
  {
    'id': 32,
    'name': 'fff',
    'code': 0
  },
  {
    'id': 43,
    'name': 'FAQ',
    'code': 0
  }
]

let arr1 = arr.map((item, i) => {
  const currentId = item.id;
  const itemsWithSameId = arr.filter(el => el.id === currentId);
  const found = itemsWithSameId.some(el => el.code === 1);
  if (found) {
    return { ...item, code: 1};
  }
  return item;
})

console.log(arr1);

Comments

1

const arr = [{
  id: 11,
  name: 'Raj',
  code: 0
}, {
  id: 11,
  name: 'Raj',
  code: 1
}, {
  id: 32,
  name: 'Krishna',
  code: 1
}, {
  id: 32,
  name: 'fff',
  code: 0
}, {
  id: 43,
  name: 'FAQ',
  code: 0
}]

const update = (id, code) => {
  const updatedArr = arr.map(el => {
    if (el.id === id) el.code = code
    return el
  })
  return updatedArr
}

const result = update(11, 1)

console.log(result)

Comments

0

let arr = [{
    'id': 11,
    'name': 'Raj',
    "code": 0
  },
  {
    'id': 11,
    'name': 'Raj',
    'code': 1
  },
  {
    'id': 32,
    'name': 'Krishna',
    'code': 1
  },
  {
    'id': 32,
    'name': 'fff',
    'code': 0
  },
  {
    'id': 43,
    'name': 'FAQ',
    'code': 0
  }
]

const foundItems = arr.filter(el => el.code === 1);

arr.forEach(el => {
  if (foundItems.find(e => e.id === el.id)) {
    el.code = 1;
  }
});

console.log(arr);

Comments

0

let arr =[
  {
    'id': 11,
    'name':'Raj',
    "code":0
  },
  {
    'id': 11,
    'name':'Raj',
    'code': 1
  },
  {
    'id':32,
    'name':'Krishna',
    'code':1
  },
  {
    'id':32,
    'name':'fff',
    'code':0
  },
  {
    'id':43,
    'name':'FAQ',
    'code':0
  }
]

   arr.forEach(el => {  if(el.code === 1){
   arr.map((obj)=>{
if(obj.id==el.id){
   obj.code=1
}
    })
  }})
 




console.log(arr)

solved

Comments

0

I would group the item based upon their id. Then filter out all groups of which at least one item has code 1. Then set the code of all items in those groups to 1.

function groupBy(iterable, fn) {
  const groups = new Map();
  for (const item of iterable) {
    const key = fn(item);
    if (!groups.has(key)) groups.set(key, []);
    groups.get(key).push(item);
  }
  return groups;
}

let arr =[
  { id: 11, name: 'Raj',     code: 0 },
  { id: 11, name: 'Raj',     code: 1 },
  { id: 32, name: 'Krishna', code: 1 },
  { id: 32, name: 'fff',     code: 0 },
  { id: 43, name: 'FAQ',     code: 0 }
];

Array.from(groupBy(arr, item => item.id).values())
     .filter(group => group.some(item => item.code == 1))
     .flat(1)
     .forEach(item => item.code = 1);

console.log(arr);

Comments

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.