0

I'm working with two arrays that has correlated the values, which mean that arrayX[1] correlate with arrayY[1], and arrayX[2] correlate with arrayY[2] and so on.

I now need to filter that arrayX, to match or contains a given value, and only displays the arrayX filtered and arrayY that matches.

I've tried to create an array/map with key - value that I found this link Take object out of array based on attribute value, and then filter with '.find' but I couldn't has the expected result.

When I call the function to filter I pass a value, for example I'm passing a string '2010'

  • My arrayX has:

    (10) ["Dec 2008", "Feb 2010", "Apr 2010", "Jul 2010", "Dec 2010", "Feb 2011", "Apr 2012", "Jul 2013", "Mar 2014", "May 2017"]

  • My arrayY has:

    (10) [1, 7, 4, 1, 10, 11, 4, 8, 7, 10]

so, for the output I need

arrayX to be ["Feb 2010", "Apr 2010", "Jul 2010", "Dec 2010"] and

arraY to be [7, 4, 1, 10].

My typescript code that expects a value and filter is:

filtr(dt){
      console.log(dt);
      console.log(this.arrayX);
      console.log(this.arrayY);

      //Array contains x and y values at the correct position
      let array = [
          {y: this.Yvalues, x: this.Xvalues}
      ];

      let item : any = array.find(i => i.x.find(dt));

      //new filtered x and y values on array 
      console.log(item)
}

I can't figure out how am I going to do this... I thought that the key -> value was a good solution, but I can't even have an output.. Sorry if I'm missing something easy.

2 Answers 2

2

You can use array#reduce and check if the provided value exist in the current date string. Then add the date and value corresponding to that index from the other array to your result array.

const arrayX = ["Dec 2008", "Feb 2010", "Apr 2010", "Jul 2010", "Dec 2010", "Feb 2011", "Apr 2012", "Jul 2013", "Mar 2014", "May 2017"],
      arrayY = [1, 7, 4, 1, 10, 11, 4, 8, 7, 10],
      year = 2010,
      result = arrayX.reduce((r, date, i) => {
        if(date.includes(year)){
          r.push({x: date, y: arrayY[i]})
        }
        return r;
      },[]);
console.log(result);

To get the X and Y values separately, for chart purposes, for example, just map the result and iterate over it, like this.

let x = result.map(({x}) => x);
let y = result.map(({y}) => y);
Sign up to request clarification or add additional context in comments.

4 Comments

Oh, we get index too!! Thanks! mine is way cumbersome than yours.
thank you friend! With this i can filter other types too right, like names, etc.. ?
Please share the sample output.
if instead of year i got names, i can filter the same way, right?
1

Please see the code snippet below. You can use the returned result like: result.newArrayX and result.newArrayY. This way you will also keep the original arrays safe.

arrayX =  ["Dec 2008", "Feb 2010", "Apr 2010", "Jul 2010", "Dec 2010", "Feb 2011", "Apr 2012", "Jul 2013", "Mar 2014", "May 2017"];

    arrayY =  [1, 7, 4, 1, 10, 11, 4, 8, 7, 10];

    function filterFunc(filterYear) {

        return this.arrayX.reduce((acc, elem) => {
                if (elem.split(' ')[1] == filterYear) {
                    acc.newArrayX.push(elem);
                    acc.newArrayY.push(this.arrayY[this.arrayX.indexOf(elem)])
                }
                return acc;
            },{newArrayX: [], newArrayY: []}
        );
    }

    result = this.filterFunc('2010');
    console.log(result);

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.