-1

I have an dataset like this:

const array = [
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sedan',
        carname: 'Audi a8',
        type: 'car'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'cruiser',
        carname: 'Harley',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sports',
        carname: 'Ninja',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sedan',
        carname: 'BMW',
        type: 'car'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sports',
        carname: 'Hayabusa',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'hatchback',
        carname: 'Micra',
        type: 'car'
    },
]

NOTE: This is raw data with shorter length. Actual data will be like 10M+ enteries. What I want is like I want to preserve the only 1 or 2 data from the array and rest bike data will be filter out from an Array.

Now for this, the method I choose is like this:

let newArray = []
let cou = 0
array.map((x) => {
    if (x.type === 'bike') {
        cou++;
        // Want to preserve only 1 data of bike
        if (cou < 2) {
            newArray.push(x)
        }
    } else {
        newArray.push(x)
    }
})

Is there any best approach for handling this type of problem. Actually I want to reduce the array iteration as it consume the memory and time of the code. Any solution or suggestion is highly appreciated. Thanks in advance for the interaction with the problem set.

Aks
  • 1,092
  • 3
  • 12
  • 29
  • So you want to leave only a single element in the array that is of type `bike`? – Silidrone Mar 24 '21 at 09:23
  • I want to preserve all car data and only 1 or 2 bike data. On code I wrote only one bike data. – Aks Mar 24 '21 at 09:24
  • It is like suppose you have 10k entry on the table and out of that 5k is for bike and 5k is for car. So wanted to get all car data and only 1k bike data. So at the end on an array, we have 6k data in which 5k is for car and 1k is for bike – Aks Mar 24 '21 at 09:26
  • 1
    Well your approach seems very efficient – Silidrone Mar 24 '21 at 09:26
  • Does this answer your question? [Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop](https://stackoverflow.com/questions/3978492/fastest-way-to-duplicate-an-array-in-javascript-slice-vs-for-loop) – Pedro Felgueiras Mar 24 '21 at 09:32
  • 1
    Why do you have 10M+ entries stored in an array to begin with? Why is this not in a database, and the selection of records gets reduced when fetching the data from there already? – CBroe Mar 24 '21 at 09:32
  • Yeah if you want efficiency you better do this with queries rather than fetching the whole data and then storing it in an array and then filtering that array, – Silidrone Mar 24 '21 at 09:34
  • @CBroe It is already on the database but for getting or discussing something we need to share the specific part not the whole story. What I feel is like if the problem statement is crystal clear, then it takes less time for the other to interact and share the thoughts. – Aks Mar 24 '21 at 09:39
  • And this data is already return form some query. What I get then this is an extra layer which will not possible to work on query due to some reason – Aks Mar 24 '21 at 09:40
  • @PedroFelgueiras it's not *duplicating* the array, it's returning a filtered sub-section of the array based on condition. – VLAZ Mar 24 '21 at 09:46
  • _“It is already on the database”_ - then why are you not filtering it _there_, when you fetch the data from the database? _“but for getting or discussing something we need to share the specific part not the whole story.”_ - but that makes it a whole _different_ topic here, when you replace “database” with “some array in JavaScript”. Filtering inside the latter will probably always perform worse - a database is _optimized_ for this kind of stuff. So this is really apples/oranges here. – CBroe Mar 24 '21 at 10:04

2 Answers2

4

You can use the filter method:

var bikeCount = 0;
var res = array.filter(x => {
  if (x.type === "bike") {
      bikeCount++;
      return bikeCount < 2;
  }
  return true;
});

In this way you don't have to create a new array and push every single element into it.

I've used an online benchmark tool and it seems 35-40% faster than your solution.

mck89
  • 18,918
  • 16
  • 89
  • 106
0

You can solve that using Array.prototype.slice method:

const array = [
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sedan',
        carname: 'Audi a8',
        type: 'car'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'cruiser',
        carname: 'Harley',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sports',
        carname: 'Ninja',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sedan',
        carname: 'BMW',
        type: 'car'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'sports',
        carname: 'Hayabusa',
        type: 'bike'
    },
    {
        id: '5c1b4ffc18e2d84b7d6feaf3',
        model: 'hatchback',
        carname: 'Micra',
        type: 'car'
    },
]

/*
  Array.prototype.slice slices an arrray and returns sliced content
  1st param : slice start index
  2nd param : slice end index
*/
const filteredArray = array.slice(0, 2);

console.log(filteredArray);
  • I think you understand wrong. Example: "It is like suppose you have 10k entry on the table and out of that 5k is for bike and 5k is for car. So wanted to get all car data and only 1k bike data. So at the end on an array, we have 6k data in which 5k is for car and 1k is for bike" – Aks Mar 24 '21 at 09:29
  • I just wanted to have open discussion about the efficient code. whether my code or approach is effiecient or do i have to make some improvements – Aks Mar 24 '21 at 09:30