0

I just want first year and last year element from Array Object

example this is the data ( it is sorted so I just don't wanna messed that up) :

[
  { make: 'audi', model: 'rs5', year: '2013' },
  { make: 'audi', model: 'rs5', year: '2015' },
  { make: 'audi', model: 'r8', year: '2020' },
  { make: 'ford', model: 'mustang', year: '2012' },
  { make: 'ford', model: 'fusion', year: '2015' },
  { make: 'ford', model: 'mustang', year: '2033' },
  { make: 'kia', model: 'optima', year: '2010' },
  { make: 'kia', model: 'optima', year: '2012' },
  { make: 'kia', model: 'optima', year: '2020' },
  { make: 'kia', model: 'optima', year: '2030' },
  { make: 'kia', model: 'optima', year: '2040' }
]

and I want it like this all the middle years removed :

[
  { make: 'audi', model: 'rs5', year: '2013' },
  { make: 'audi', model: 'r8', year: '2020' },
  { make: 'ford', model: 'mustang', year: '2012' },
  { make: 'ford', model: 'mustang', year: '2033' },
  { make: 'kia', model: 'optima', year: '2010' },
  { make: 'kia', model: 'optima', year: '2040' }
]
4
  • 5
    What have you tried so far? Commented Jan 23, 2020 at 11:33
  • Why not sort it by the years first, then by make? You could then just separate the array by make, and then grab the first and last values. Commented Jan 23, 2020 at 11:50
  • why only one object for 'rs5'? Commented Jan 23, 2020 at 11:55
  • @NinaScholz I'm assuming they're getting the latest and oldest by make, not model Commented Jan 23, 2020 at 11:56

5 Answers 5

3

You could just separate the array by make, and then grab the first and last values of each make:

const arr = [
  { make: 'audi', model: 'rs5', year: '2013' },
  { make: 'audi', model: 'rs5', year: '2015' },
  { make: 'audi', model: 'r8', year: '2020' },
  { make: 'ford', model: 'mustang', year: '2012' },
  { make: 'ford', model: 'fusion', year: '2015' },
  { make: 'ford', model: 'mustang', year: '2033' },
  { make: 'kia', model: 'optima', year: '2010' },
  { make: 'kia', model: 'optima', year: '2012' },
  { make: 'kia', model: 'optima', year: '2020' },
  { make: 'kia', model: 'optima', year: '2030' },
  { make: 'kia', model: 'optima', year: '2040' }
]

const grouped = arr.reduce((a, el) => (a[el.make] ? a[el.make].push(el) : a[el.make] = [el], a), {})
const out = Object.values(grouped).reduce((a, group) => (a.push(group[0], group[group.length - 1]), a), [])

console.log(out)

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

3 Comments

OP hasn't specified how to handle that case, I'm not sure which one they would want to pick. Unless I've missed something...
Thanks Kobe it really helped me a lot.
@SRTechConnect No problem, if you need more help, feel free to ask :)
2

You could reduce the array by looking for the next to last item and if it has the same make as the actual object, then update the last index.

Otherwise push the object to the result set.

This approach uses the sorted array by make and year.

var data = [{ make: 'audi', model: 'rs5', year: '2013' }, { make: 'audi', model: 'rs5', year: '2015' }, { make: 'audi', model: 'r8', year: '2020' }, { make: 'ford', model: 'mustang', year: '2012' }, { make: 'ford', model: 'fusion', year: '2015' }, { make: 'ford', model: 'mustang', year: '2033' }, { make: 'kia', model: 'optima', year: '2010' }, { make: 'kia', model: 'optima', year: '2012' }, { make: 'kia', model: 'optima', year: '2020' }, { make: 'kia', model: 'optima', year: '2030' }, { make: 'kia', model: 'optima', year: '2040' }],
    result = data.reduce((r, o) => {
        if ((r[r.length - 2] || {}).make === o.make) r[r.length - 1] = o; 
        else r.push(o);
        return r;
    }, []);

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

Comments

0

If your array is sorted then try this

var newcar = [];
var myvar = [];
for (var i=0; i<(car.length)-1; i++) {
    if(car[i].make == car[i+1].make) 
    {
        myvar.push(car[i]);
        if(i+1 == car.length-1)
        {
            myvar.push(car[i+1]);
            newcar.push(myvar[0]);
            var lastitem = myvar.pop();
            newcar.push(lastitem);
        }
    }
    else
    {
        myvar.push(car[i]);
        newcar.push(myvar[0]);
        var lastitem = myvar.pop();
        newcar.push(lastitem);
        myvar = [];
    }
}

Comments

-1

use filter method on the array, you can find docs in the following https://www.geeksforgeeks.org/es6-array-filter-method/ and

example:

const arr = [1,2,3,4,5];
console.log(arr.filter((i) => {
// A Condition, if true it's displayed if not it's discarded
return i > 2;
}));

Comments

-2

You can loop through the array and save the ones with the maximum and minimum year like this:

let data = [
  { make: 'audi', model: 'rs5', year: '2013' },
  { make: 'audi', model: 'rs5', year: '2015' },
  { make: 'audi', model: 'r8', year: '2020' },
  { make: 'ford', model: 'mustang', year: '2012' },
  { make: 'ford', model: 'fusion', year: '2015' },
  { make: 'ford', model: 'mustang', year: '2033' },
  { make: 'kia', model: 'optima', year: '2010' },
  { make: 'kia', model: 'optima', year: '2012' },
  { make: 'kia', model: 'optima', year: '2020' },
  { make: 'kia', model: 'optima', year: '2030' },
  { make: 'kia', model: 'optima', year: '2040' }
];

let a = {};

for(let i = 0; i<data.length; i++) {
  if( !a[data[i].make] ) {
    a[data[i].make] = {};
  }
  if( !a[data[i].make].max || a[data[i].make].max.year < data[i].year ) {
    a[data[i].make].max = data[i];
  }
  if( !a[data[i].make].min || a[data[i].make].min.year > data[i].year ) {
    a[data[i].make].min = data[i];
  }
}

let output = [];

Object.keys(a).forEach(make => {
  output.push( a[make].min );
  output.push( a[make].max );
});

console.dir( output );

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.