skip to Main Content

I have array of object listItem, passing parameter paramId

Based on condition, when the id in the the list is same with paramId and having color as red

then next object (current val greater than val) value should be like

value, value + 1, value + 2, value + 3… and that current object val become null

how to achieve this using for.. of loop using javascript


example 1
var paramId = 14
const listItem = [
  { id: 10, val: null, color: 'blue' },
  { id: 13, val: 1, color: 'black' },
  { id: 14, val: 2, color: 'red' },
  { id: 25, val: 3, color: 'yellow' }, 
  { id: 43, val: 5, color: 'green' },

];


Expected Output

[
  { id: 10, val: null, color: 'blue' },
  { id: 13, val: 1, color: 'black' },
  { id: 14, val: null, color: 'red' }, // change val to null
  { id: 25, val: 2, color: 'yellow' }, // 3 becomes 2 (val)
  { id: 43, val: 3, color: 'green' }, //5 becomes 3 (val + 1)

];

example 2
var paramId = 25
const listItem = [
  { id: 10, val: null, color: 'blue' },
  { id: 13, val: 1, color: 'black' },
  { id: 14, val: 2, color: 'yellow' },
  { id: 25, val: 3, color: 'red' }, 
  { id: 23, val: 4, color: 'green' },
  { id: 43, val: 5, color: 'green' },

];

Expected Output

[
  { id: 10, val: null, color: 'blue' },
  { id: 13, val: 1, color: 'black' },
  { id: 14, val: 2, color: 'yellow' },
  { id: 25, val: null, color: 'red' }, // changes val to null
  { id: 23, val: 3, color: 'green' }, // 4 becomes 3 (val)
  { id: 43, val: 4, color: 'green' }, // 5 becomes 4 (val+1)

];

Tried


const getByIdandColor =  list.find(e=>e.id === paramId and e.color === 'red')


for (const item of listItem) {
   if(item.id === paramId && item.color === 'red') {
     ...item,
     val: null
   } else if(item.color !== 'red' && item.id >= getByIdandColor.id) {
      ...item,
     val: val +1
   }
 
}

4

Answers


  1. hey im not sure but i think you can handle it like that.

    for (const item of listItem) {
      if(item.id === paramId && item.color === 'red') {
        ...item,
        item.val: null // val of what?
      } else if(item.color !== 'red' && item.id >= getByIdandColor.id) {
        ...item,
        item.val: item.val +1 // val of what?
      }
    }
    

    You have to call the val of the currenty item. Otherwise val will not be find and so you cant update any value of undefined ^^

    Login or Signup to reply.
  2. The problem is your are not changing the value on the list, try this.

    let oldValue;
    for (const item of listItem) {
      if (item.id === paramId && item.color === "red") {
        oldValue = item.val // set old value
        const index = listItem.indexOf(item); // get the index of the current item on the list
        listItem[index] = { // change the item on the list
          ...item,
          val: null,
        };
      } else if (item.color !== "red" && item.id >= getByIdandColor.id) {
        const index = listItem.indexOf(item);
    
        listItem[index] = {
          ...item,
          val: oldValue,
        };
        oldValue++
      }
    }
    
    Login or Signup to reply.
  3. let paramId = 25
    const listItem = [
      { id: 10, val: null, color: 'blue' },
      { id: 13, val: 1, color: 'black' },
      { id: 14, val: 2, color: 'yellow' },
      { id: 25, val: 3, color: 'red' }, 
      { id: 23, val: 4, color: 'green' },
      { id: 43, val: 5, color: 'green' },
    
    ];
    
    
    const getByIdandColor =  listItem.find(e=>e.id === paramId && e.color === 'red')
    
    
    let prevIndex;
    const x = listItem.map((item, index)=> {
        if(item.id === paramId && item.color === 'red'){
            prevIndex = index
            return {...item, val:null}
        }
        if(item.color !== 'red' && index === prevIndex + 1){
            prevIndex = index
            return {...item, val : listItem[prevIndex].val -1}
        }
        return {...item}
    })
    
    console.log(x)
    Login or Signup to reply.
  4. I have created a function which satisfies the expected results.
    Here it goes like this.

        function getLastValidValue(listArr, index) {
          // in this function we are getting last valid value from the array before given index
          let lastValidValue = null;
          for (let i = index - 1; i >= 0; i--) {
            if (listArr[i].val !== null) {
              lastValidValue = listArr[i].val;
              break;
            }
          }
          return lastValidValue;
        }
        
        function getExpectedOutput(listItem, id) {
          // using a blank array to store values
          const output = [];
        
          for (let i = 0; i < listItem.length; i++) {
            // if the id and color matches we are pushing the object with null value
            if (listItem[i].id === id && listItem[i].color === 'red') {
              output.push({ ...listItem[i], val: null });
            }
            // if the id does not match and item value is null we are pushing the object as it is
            if (listItem[i].id !== id && listItem[i].val === null) {
              output.push({ ...listItem[i] });
            }
            // if the id does not match and item value is not null we are pushing the object with last valid value + 1
            if (listItem[i].id !== id && listItem[i].val !== null) {
              output.push({ ...listItem[i], val: getLastValidValue(output, i) ? getLastValidValue(output, i) + 1 : 1 });
            }
          }
          return output;
        }
        const paramId = 14;
    
        const listItems = [
          { id: 10, val: null, color: 'blue' },
          { id: 13, val: 1, color: 'black' },
          { id: 14, val: 2, color: 'red' },
          { id: 25, val: 3, color: 'yellow' },
          { id: 43, val: 5, color: 'green' },
        ];
    
        console.log(getExpectedOutput(listItems, paramId));
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search