0

I need to be able to remove object from object based on array values.

I have a base object:

   const obj = {
      one: {
        selected: undefined,
      }, 
      two: {
        selected: undefined,
      },
      three: {
        selected: undefined,
      }
    };

Example array is:

const arr = ['one', 'two'];

My attempt at doing this:

const mappedObj = arr.map(val => {
  if (obj[val]) {
    return ({
      [val]: {
        selected: true
      },
    });
  }

  return false;
})

I need it to be:

object: {
  one: {
    selected: true,
  },
  two: {
   selected: true,
  }
}
2
  • How is it determined that one.selected is true and two.selected is false as per your expected result? Commented Aug 9, 2018 at 12:54
  • @charlietfl sorry, it should be true Commented Aug 9, 2018 at 12:56

6 Answers 6

1

You could map single objects and assign them to a single object with Object.assign and spread syntax ... for the array.

var object = { one: { selected: undefined, foo: 42 }, two: { selected: undefined }, three: { selected: undefined } },
    array = ['one', 'two'],
    result = Object.assign(
        ...array.map(k => ({ [k]: Object.assign({}, object[k], { selected: true }) }))
    );
  
console.log(result);

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

2 Comments

selected is not set to true
I'd also suggest just in case: { ...obj[k], selected: true }
0

You can try with reduce on arr array:

const result = arr.reduce((acc, key) => {
  if (obj[key]) {
    acc[key] = { ...obj[key], selected: true };
  }
  return acc
}, {});

Comments

0

You can use simple forEach() loop:

const obj = {
      one: {
        selected: undefined,
      }, 
      two: {
        selected: undefined,
      },
      three: {
        selected: undefined,
      }
    };

const arr = ['one', 'two'];
Object.keys(obj).forEach((key) => obj[key].selected = arr.includes(key));
console.log(obj);

Comments

0

You can use "reduce" instead of "map":

const obj = {
      one: {
        selected: undefined,
      }, 
      two: {
        selected: undefined,
      },
      three: {
        selected: undefined,
      }
    };
const arr = ['one', 'two'];
const mappedObj = arr.reduce((acm, val) => { 
    if (val in obj) { 
      acm[val] = { selected: true };
    }
    return acm;
  }, 
  {}
);

result:

{ one: { selected: true }, two: { selected: true } }

Comments

0

const obj = {
      one: {
        selected: undefined,
      }, 
      two: {
        selected: undefined,
      },
      three: {
        selected: undefined,
      }
    };

const arr = ['one', 'two'];

Object.keys(obj).map((item) => { 
       if(arr.includes(item)){
         obj[item].selected = true
       } else { 
        delete obj[item]
       }
     })

 console.log(obj)

Comments

0

Using one line reduce()

const obj = {
      one: {
        selected: undefined,
      }, 
      two: {
        selected: undefined,
      },
      three: {
        selected: undefined,
      }
    };



const arr = ['one', 'two'];



const res = 
   arr.reduce((a,c) => (obj[c] && (a[c]= Object.assign({}, obj[c], {selected:true})),a),{});
 

console.log(res)

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.