0

I have the following object:

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  condition: 'checked' // could be "unckecked" or anything else
  items: []
}

and I want to check if the condition === checked then I will have to move a and b inside items like this:

const obj = {
  c: 3,
  d: 4,
  condition: 'checked' // could be "unckecked" or anything else
  items: [{
       a: 1,
       b: 2,
  }]
}

I have tried splice and tried to loop through the object with for(of) but didn't manage to do it, any idea how?

6
  • Sounds reasonable. What have you tried? Hint: delete or create a new object and populate it. Commented Mar 26, 2021 at 12:07
  • Welcome to Stack Overflow! Visit the help center, take the tour to see what and How to Ask. If you get stuck, post a minimal reproducible example of your attempt, noting input and expected output using the [<>] snippet editor. Commented Mar 26, 2021 at 12:08
  • @todman I have tried that but I tried to avoid the delete actually, is there any other way? Commented Mar 26, 2021 at 12:10
  • check if( obj.condition === 'checked' ) and append the values to obj.items? Commented Mar 26, 2021 at 12:11
  • @Errand you want to push a and b or push the first and the second property? I mean a always called a and b always called b in any case? Commented Mar 26, 2021 at 12:12

3 Answers 3

2

If you don't want to use delete, you can use a function to return a new object instead with the values moved as required:

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  condition: 'checked', // could be "unckecked" or anything else
  items: []
}

const moveit = ({ a, b, condition, items, ...rest}) => 
condition === 'checked' ? { ...rest, condition, items: [{ a, b }] } : { a, b, ...rest, condition, items };

console.log(moveit(obj))

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

2 Comments

that would do the job! nice one :)
@Errand I'm glad it helped! :)
2

If you do not want delete, try a reduce

You could make this a little more DRY if you mapped the keys to the item

let  obj = { a: 1, b: 2, c: 3, d: 4, condition: 'checked',  items: [] };

if (obj.condition ==="checked") {
  const keys = ["a","b"]
  obj = Object.entries(obj).reduce((acc,[key,val]) => {
    if (key === "items") acc[key] = [{ a:obj.a,  b:obj.b}];
    else if (!keys.includes(key)) acc[key] = val;
    return acc;
  },{})
}  

console.log(obj)

Older answer:

let  obj = { a: 1, b: 2, c: 3, d: 4, condition: 'checked',  items: [] };

if (obj.condition ==="checked") {
  obj.items.push({ a:obj.a,  b:obj.b})
  delete obj.a;
  delete obj.b;
}  

console.log(obj)

Comments

1

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  condition: 'checked',
  items: []
};

// If condition equals 'checked'
if (obj.condition === 'checked') {
    
    // Move a and b into items
    obj.items.push({ 
      a: obj.a,
      b: obj.b
    });
    
    // Delete old key/values
    delete obj.a;
    delete obj.b;
}

// Show result
console.log(obj);

6 Comments

Thanks for the answer but I have tried this one but is there a way without delete?
You'll need delete to remove the old a and b. key from obj
but is there a way where I can move them and not really delete after pushing to the array?
So obj.a and obj.b should stay intact? Just remove the delete call
Please update your question with the desired output, currently it's showing a new object without obj.a and obj.b
|

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.