skip to Main Content

do you know how to get array like this

[
    {
        index: 4,
        years: [1999, 2001, 2012, 2020, 1999, 1998, 2020],
        price: 200
    },
    {
        index: 2,
        years: [1999, 2019, 2020],
        price: 0
    },
        {
        index: 3,
        years: [2020, 1999, 2019, 2020],
        price: 100
    }
]

with this data

[
    {
        index: 4,
        years: [1999, 1998, 2020],
        price: 100
    },
    {
        index: 4,
        years: [1999, 2001, 2012, 2020],
        price: 200
    },
    {
        index: 2,
        years: [1999, 2019, 2020],
        price: 0
    },
    {
        index: 3,
        years: [1999, 2019, 2020],
        price: 0
    },
    {
        index: 3,
        years: [2020],
        price: 100
    }
    
]

I would like to get the highest price value for the same index and connect the year arrays.

I have no idea how to get such a result.

2

Answers


  1. Collect items with the same index in a map. If an item with a bigger price is found, replace the previous item. If the price is the same merge the years with a set:

    const result = [...arr.reduce((map, item) => {
      const found = map.get(item.index);
      if(!found || found.price < item.price){
          map.set(item.index, item);
      } else if (found.price === item.price){ // merge years
          found.years = [...new Set([...found.years, ...item.years])];
      }
      return map;
    }, new Map).values()];
    
    console.log(result);
    <script>
    const arr = [
        {
            index: 4,
            years: [1999, 2001, 2012, 2020, 1999, 1998, 2020],
            price: 200
        },
        {
            index: 2,
            years: [1999, 2019, 2020],
            price: 0
        },
            {
            index: 3,
            years: [2020, 1999, 2019, 2020],
            price: 100
        },
        {
            index: 4,
            years: [1999, 1998, 2020],
            price: 100
        },
        {
            index: 4,
            years: [1999, 2001, 2012, 2020],
            price: 200
        },
        {
            index: 2,
            years: [1999, 2019, 2020],
            price: 0
        },
        {
            index: 3,
            years: [1999, 2019, 2020],
            price: 0
        },
        {
            index: 3,
            years: [2020],
            price: 100
        }
    ]
    </script>
    Login or Signup to reply.
  2. I created a custom reducer below that accepts an accessor (key or function); a reducer function that takes the existing value, and the current value; and a dataArr (array of item arrays). The first object has priority, so only its price will update.

    const main = () => {
      const accessor = ({ index }) => index; // or simply 'index'
      const reducer = (existing, item) => {
        existing.price = Math.max(existing.price, item.price);
        return existing;
      };
      const c = combine(accessor, reducer, a, b);
      console.log(c.map(item => JSON.stringify(item)).join('n'));
    };
    
    const combine = (accessor, reducer, ...dataArr) => {
      const lookup = new Map();
      for (const data of dataArr) {
        for (let item of data) {
          const k = accessor instanceof Function ? accessor(item) : item[accessor];
          const existing = lookup.get(k);
          lookup.set(k, existing ? reducer(existing, item) : structuredClone(item));
        }
      }
      return [...lookup.values()];
    };
    
    const a = [{
      index: 4,
      years: [1999, 2001, 2012, 2020, 1999, 1998, 2020],
      price: 200
    }, {
      index: 2,
      years: [1999, 2019, 2020],
      price: 0
    }, {
      index: 3,
      years: [2020, 1999, 2019, 2020],
      price: 100
    }];
    
    const b = [{
      index: 4,
      years: [1999, 1998, 2020],
      price: 100
    }, {
      index: 4,
      years: [1999, 2001, 2012, 2020],
      price: 200
    }, {
      index: 2,
      years: [1999, 2019, 2020],
      price: 0
    }, {
      index: 3,
      years: [1999, 2019, 2020],
      price: 0
    }, {
      index: 3,
      years: [2020],
      price: 100
    }];
    
    main();
    .as-console-wrapper { top: 0; max-height: 100% !important; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search