0

I'd like to filter and restructure some specific data, but I'm having some trouble getting it to produce the proper structure.

I have this data response in this structure....

var response = {
   badProperty1: "bad property text"
   goodProperty1: "abc"
   goodProperty2: "bcd"
   goodProperty3: "cde"
   goodProperty4: "def"
   goodProperty5: "efg"
   badProperty2: "fgh"
};

I'd like to convert my response object into an array like this structure exactly...

var newFilteredStructuredArray = [
   {goodProperty1: 'abc'},
   {goodProperty2: 'bcd'},
   {goodProperty3: 'cde'},
   {goodProperty4: 'def'},
   {goodProperty5: 'fgh'}
];

However I'd like to filter out the bad properties before I get my new array. I'd like my array to only include these specific property names and filter everything else that I started with.

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
1
  • 1
    What's the actual problem/question? Iterate over possiblePropertyNames and use its elements to produce the expected output. Array.prototype.map() might be useful. Commented Mar 28, 2022 at 17:20

1 Answer 1

3

an implementation using filter and map on the object entries of response. In the filter stage I filter out only if it is available in possiblePropertyNames. Then in the map stage I return the array in the format you need

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = Object.entries(response).filter(([k,v]) => possiblePropertyNames.includes(k)).map(([k,v]) => ({[k]:v}))

console.log(newFilteredStructuredArray)

without es6

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = Object.keys(response)
.filter(function(a){
    return possiblePropertyNames.includes(a)
})
.map(function(b){
    return {[b]:response[b]}
})

console.log(newFilteredStructuredArray)

UPDATE better answer with just a map on the possiblePropertyNames array as pointed out by Andreas in the comments

var possiblePropertyNames = ['goodProperty1', 'goodProperty2', 'goodProperty3', 'goodProperty4', 'goodProperty5'];
var response = {badProperty1: "bad property text",goodProperty1: "abc",goodProperty2: "bcd",goodProperty3: "cde",goodProperty4: "def",goodProperty5: "efg",badProperty2: "fgh"}

var newFilteredStructuredArray = possiblePropertyNames.map(prop => ({[prop]:response[prop]}))

console.log(newFilteredStructuredArray)

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

7 Comments

I was about to post a similar answer, but using a for...in loop. Yet, I like your answer using Object.entries() much better. +1
Awesome yes this exactly what I'm looking for. I'm not quite using ES6 could you convert this to not be ES6? Thanks
@user7814645 i removed destructuring, arrow fucntions. Object.entries() is not es6 but Im not sure. anyway Object.keys should work
@user7814645 tbh not sure of includes as well. need to check
Why Object.entries()/.keys() + Array.prototype.filter()? The "good" properties are all listed in possiblePropertyNames. Just iterate over that array: possiblePropertyNames.reduce(...). One method vs. three (you don't need .map() with .reduce())
|

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.