0

Considering follow code

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
}

const resultWantToGet = [
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  }
]

FilterArray is a array to be used as a filter index, objectToBeFilter is a object which I want to handle. How to use filterArray to filter objectToBeFilter and then convert it to a array resultWantToGet?

4
  • You say you want an array, but the "array" you are showing has keys a and b, which is a syntax error. Do you want an array or new object? Commented Sep 17, 2018 at 3:39
  • The expected result is wrong Commented Sep 17, 2018 at 3:40
  • The resultWantToGet is a array contain many objects, doesn't it? Commented Sep 17, 2018 at 4:10
  • 1
    No, @yuanlai, an array of objects will look like this: [ { ab: 'ab', ac: 'ac', ad: 'ad' }, { bb: 'bb', bc: 'bc', bd: 'bd' } ] (without the keys). See answer below if that's what you are looking for. Commented Sep 17, 2018 at 4:26

4 Answers 4

1

Based on your last comment, it seem like you want an array of objects. You can do this simply with, which will give you the objects from objectToBeFilter keyed at a and b in an array:

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
}


let result = filterArray.map(key => objectToBeFilter[key])
console.log(result)

This looks different than your expected result because arrays don't have keys. If you want each item to be identified with a key, you probably want an object, but if you want an ordered collection, then an array is appropriate.

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

Comments

1

You can map by filterArray and spread into a new object:

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
};
console.log({
  ...filterArray.map(key => ({ [key]: objectToBeFilter[key] }))
});

Or, if you don't want to create intermediate objects, use reduce:

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
};
console.log(filterArray.reduce((a, prop) => {
  a[prop] = objectToBeFilter[prop];
  return a;
}, {}));

Comments

1

You can use reduce to loop and construct new object.

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
}

const resultWantToGet = filterArray.reduce((c, v) => {
  if (objectToBeFilter[v]) c[v] = objectToBeFilter[v]; //Check if the key exist in objectToBeFilter, if it does, assign to the accumulator. 
  return c;
}, {})

console.log(resultWantToGet);

4 Comments

Sorry for disturbing, how to convert resultWantToGet to a array?
@yuanlai, You need numerical indices?
Because I want to use length and resultWantToGet[index] like method, so I have this idea.
You can use Object.values to get all values. If you want to get the length:Object.values(resultWantToGet).length
1

you can use the following code Object.keys(objectToBeFilter) get list of filtered object keys then filter keys that included in filter array .filter(key => filterArray.includes(key)) then construct a new object with only the filtered properties reduce((obj, key)

const filterArray = ['a', 'b']
const objectToBeFilter = {
  a: {
    ab: 'ab',
    ac: 'ac',
    ad: 'ad'
  },
  b: {
    bb: 'bb',
    bc: 'bc',
    bd: 'bd'
  },
  c: {
    cb: 'cb',
    cc: 'cc',
    cd: 'cd'
  }
}

const resultWantToGet =  Object.keys(objectToBeFilter)
  .filter(key => filterArray.includes(key))
  .reduce((obj, key) => {
    obj[key] = objectToBeFilter[key];
    return obj;}, {});

console.log(resultWantToGet)

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.