0

I have trouble to map through a nested object with string and objects, trying to get a list of the "qty" value in array so I can filter out.

data looks like this:

const data = {   
   '123': {
        'name': 'Part 1',
        'size': '20',
        'qty' : '50'
    },
    '5678' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '60'
    },
   '9810' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '120'
    },
 }

// my code I tried:
const getValue = Object.key(data).map(i=> [i].qty)  //undefined
// expect return ['50','60','120']

const items = ['4','120','5']

// Expecting remove '120' from this list, because the getValue should have '120' in the array.
const filterItem = items.filter(i => i !== getValue)

Thanks for the help!

1
  • 3
    Should be Object.keys(data).map(i => data[i].qty). Your current code is creating a new array literal and trying to retrieve the qty property of the array, which doesn't exist. Commented Jul 25, 2019 at 21:50

2 Answers 2

1

Try like this

Object.keys(data).map(i => data[i].qty)

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

Comments

0

You can do that simply as below, check the comments to understand better:

const extractQuantityValues = ['120'];

const newDataValues = Object.keys(data)
 // select keys of object and keep them as array type
 .filter( i => !extractQuantityValues.includes(data[i].qty) )
 // filter the keys to get needed values that kept above,
 // if it is have no values of the your extractQuantityValues array
 .reduce( (res, key) => (res[key] = data[key], res), {} );
 // create new object which you need filtered values of object from the data object

And check the snippet which it works exactly what you need:

const data = {   
   '123': {
        'name': 'Part 1',
        'size': '20',
        'qty' : '50'
    },
    '5678' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '60'
    },
   '9810' : {
        'name': 'Part 2',
        'size': '15',
        'qty' : '120'
    },
 }
 
const extractQuantityValues = ['120'];

const newDataValues = Object.keys(data)
 .filter( i => !extractQuantityValues.includes(data[i].qty) )
 .reduce( (res, key) => (res[key] = data[key], res), {} );

console.log(newDataValues);

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.