JS ES6 Correct way to filter object by array of keys

javascript filter object by key value
javascript filter array of objects by another array of objects
es6 filter array of objects
lodash filter object
filter multidimensional array javascript
javascript filter array multiple values
object.filter is not a function
string filter javascript

I would like to take an array of objects and filter each object to only return the properties where the key matches an item in an array.

For example:

const myKeys    = ['key_1', 'key_3'];
const myArray   = [
    { 
        key_1: 'Some Value A',
        key_2: 'Some Other Value A', 
        key_3: 'Some Final Value A',
    },
    { 
        key_1: 'Some Value B',
        key_2: 'Some Other Value B', 
        key_3: 'Some Final Value B',
    },
    { 
        key_1: 'Some Value C',
        key_2: 'Some Other Value C', 
        key_3: 'Some Final Value C',
    },
];

Should produce the following result:

const result   = [
    { 
        key_1: 'Some Value A',
        key_3: 'Some Final Value A',
    },
    { 
        key_1: 'Some Value B',
        key_3: 'Some Final Value B',
    },
    { 
        key_1: 'Some Value C',
        key_3: 'Some Final Value C',
    },
];

I believe this should be possible with a combination of .map() and .filter() but I'm unsure how best to achieve the equivalent of the following using ES6:

const filteredData = (array, keys) => {
    const newArr = [];

    for (let i = 0; i < myArray.length; i++) {
        const item      = myArray[i];
        let newObj      = {};

        for (let j = 0; j < myKeys.length; j++) {
            const filter = myKeys[j];
            newObj[filter] = item[filter];
        }

        newArr.push(newObj);
    }

    return newArr;
}

console.log(filteredData(myArray, myKeys));

You could map the wnated entries and builds objects with it.

const 
    keys = ['key_1', 'key_3'],
    data = [{ key_1: 'Some Value A', key_2: 'Some Other Value A', key_3: 'Some Final Value A' }, { key_1: 'Some Value B', key_2: 'Some Other Value B', key_3: 'Some Final Value B' }, { key_1: 'Some Value C', key_2: 'Some Other Value C', key_3: 'Some Final Value C' }],
    result = data.map(o => Object.fromEntries(keys.map(k => [k, o[k]])));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

JavaScript: manipulating objects with Object.keys() – clubmate.fi, Filed under: JavaScript—Tags: array, forEach, loop, object The Object.keys() method returns an array of a given object's own Array.prototype.filter(). 4. ARMD —Delete some key’s from object. Note: We will actually not delete the key but return a new object, if you want to delete the key use delete operator, here we are considering object immutability. To delete keys there are lot of ways but we will look at the most easy, single lined. Lets try to delete website from users.

You can simply loop over keys and build a new object which contains only those keys and respective values

const myKeys = ['key_1', 'key_3'];
const myArray = [{key_1: 'Some Value A',key_2: 'Some Other Value A',key_3: 'Some Final Value A',},{key_1: 'Some Value B',key_2: 'Some Other Value B',key_3: 'Some Final Value B',},{key_1: 'Some Value C',key_2: 'Some Other Value C',key_3: 'Some Final Value C',},];

let final = myArray.map(v => {
  return myKeys.reduce((op, key) => {
    op[key] = v[key]
    return op
  }, {})
})

console.log(final)

Array.prototype.filter(), If you are a fan of javascript and use it daily th n you will love this Javascript is telling the computer what we want it to do, we are instructing it on how to do it. Filter works on array return an array for filtered items. The use es6 of destructuring of object keys and map to get the contact info array for user. And yesterday, we looked at the ES6 way to loop through arrays and NodeLists. Today, let’s look at the ES6 approach to looping through objects. Object.keys() and Array.forEach() Strangely, there is no Object.forEach() method. To achieve something similar, we can use the Object.keys() method, which returns an array of the keys in an object

const myKeys    = ['key_1', 'key_3'];
const myArray   = [
	{
		key_1: 'Some Value A',
		key_2: 'Some Other Value A',
		key_3: 'Some Final Value A',
	},
	{
		key_1: 'Some Value B',
		key_2: 'Some Other Value B',
		key_3: 'Some Final Value B',
	},
	{
		key_1: 'Some Value C',
		key_2: 'Some Other Value C',
		key_3: 'Some Final Value C',
	},
];

const result = myArray.map(i => Object.fromEntries(Object.entries(i).filter(([k]) => myKeys.includes(k))));

console.log(result);

Map-Reduce-Filter-Find In Javascript ES6, Use var myKeys = Object.keys(myObject) to get the keys. Check if a myString exist in the array myKeys using native var matchingKey = myKeys. Basically this uses the reduce function to pass in an accumulator and current item, which then uses this to build your "grouped" arrays based on the passed in key. the inner part of the reduce may look complicated but essentially it is testing to see if the key of the passed in object exists and if it doesn't then create an empty array and

You can .map each object to its entries so you can .filter out any entries which don't have a key in myKeys, and then use .fromEntries() to build up your new object like so:

const myKeys = ['key_1', 'key_3'];
const myArray = [{
    key_1: 'Some Value A',
    key_2: 'Some Other Value A',
    key_3: 'Some Final Value A',
  },
  {
    key_1: 'Some Value B',
    key_2: 'Some Other Value B',
    key_3: 'Some Final Value B',
  },
  {
    key_1: 'Some Value C',
    key_2: 'Some Other Value C',
    key_3: 'Some Final Value C',
  },
];

const res = myArray.map(obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => myKeys.includes(k))));

console.log(res);

Quickly filter an object by keys, We are telling the computer what we want it to do rather how to do it. The filter works on an array to return an array for filtered items. The use of ES6 of destructuring of object keys and map to get the contact info array for the user. Follow all the topics you care about, and we'll deliver the best stories for  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more ES6 map an array of objects, to return an array of objects with new keys [duplicate]

Javascript data structure with map, reduce, filter, and ES6, This tutorial shows you how to use the JavaScript array filter method to filter elements in an array based on a specified Suppose you have an array of city objects where each object contains two properties: name and population . In ES6, it is even cleaner when you use the arrow function ( => ). All Right Reserved. Object.keys(obj) Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object. Description. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object

JavaScript Array Filter: Filtering Array Elements Based on a Test , Get code examples like "lodash filter object keys" instantly right from your google search results with javascript by Grepper on Nov 06 2019 Donate Learn how Grepper helps you improve as a Developer! Use the method Array. es6 closures · ES6: Declare a Read-Only Variable with the const KeywordPassed · ES6:  As you can see, using .reduce() is an easy way to generate a single value or object from an array. .filter() What if you have an array, but only want some of the elements in it?

lodash filter object keys Code Example, How to Create ToDo App using HTML, CSS, JS and Bootstrap ? How to Sort/​Order keys in JavaScript objects ? Introduction to ElectronJS · How to Create Dark/  # Converting Object to an Array. Finally, with ES2017, it's official now! We have 3 variations to convert an Object to an Array 🎊 Array has an array of methods (sorry, bad pun 😝). So by converting the object into an array, you have access to all of that. Woohoo 🥳