1

enter image description here

I´m trying to filter the data but i have an array inside an array, and i have no idea how can i get those! Also there is a property called 'toString' and thats might be a problem (function)

        const filterteste = this.state.example.changelog.histories.map(item => (
        {
          id: item.id,            
          created: item.created,
          field: item.items.field,
          fromStringprop: item.items.fromString,
          toStringprop: item.items.toString,
        }
      ));

Desired Ouput: I´m trying to filter the array soo i have a clean array with the properties i will need

{
   created: 
   id:
   field:
   fromString:
   toString:
}

Problem: enter image description here

6
  • You need to explain more specifically what you want to do. Filter what exactly? What is the expected output? Clarify a bit please. Commented Jun 11, 2018 at 10:03
  • In your .map handler, you can call .map(item.items) to map the child array, or any other Array method (do you maybe need something like .reduce or .filter for the child array?) Commented Jun 11, 2018 at 10:08
  • Hi @Chris ! I´m trying to have a clean array with the properties i need only! Commented Jun 11, 2018 at 10:09
  • How would you handle the author with id 56617 that seems to have an array of 2 items? How does that datastructure look like? Commented Jun 11, 2018 at 10:10
  • @Icepickle i´ve deleted Author property but its an object Author {name:example, id: example}, but i´m not going to need it! Commented Jun 11, 2018 at 10:15

2 Answers 2

2
const filterteste = this.state.example.changelog.histories.map(item => {
    //instead of returning a single item, we return an array!
    return item.items.map(childItem => {
        return {
            id: item.id,            
            created: item.created,
            field: childItem.field,
            fromStringprop: childItem.fromString,
            toStringprop: childItem.toString,
        };
    });
});

With this code you're mapping the base array elements, and each of these elements is returned as a map of the child elements - does this make sense?

After this you will still have an array, like so:

[
    [
        //item1,
        //item2
    ],
    [
        //item3
    ]
]

At this point we can just call reduce on this array to get to our expected result...

let resultOfArrayMap = theMapFunctionFromTopOfThePost(baseArray);
const properlyMappedArray = resultOfArrayMap.reduce((arrayBeingBuilt, currValue) => {
    return arrayBeingBuilt.concat(...currValue);
}, []);

This starts from an empty array, for each item in our mapped array it concats all of the child items in a new array, then proceeds to the next item and keeps contatenating other items to the array returned by processing the previous element...

It should now look like this:

[
    //item1,
    //item2,
    //item3
]
Sign up to request clarification or add additional context in comments.

Comments

1

You could simply filter out them as

 const filterteste = this.state.example.changelog.histories.map(item => (
        {
          id: item.id,            
          created: item.created,
          field: item.items.field,
          fromStringprop: item.items.filter((item)=> return item.fromString != null),
          toStringprop: item.items.filter((item)=> return item.toString != null)
        }
      ));

1 Comment

would you please make sure that item.items is not stringify?

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.