最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - filter objects in many arrays with same id - Stack Overflow

programmeradmin2浏览0评论

I have a nice puzzle : I have the following JavaScript array

var json = {
  'cars': [
    {
      "car_id": "1",
      "price": "925",
      "full_option": "1100"
      "date_price": 04-05-2016
      ...    
    }, 
    {
      "car_id": "1",
      "price": "990",
      "full_option": "1200"
      "date_price": 04-05-2015
      ....
    },
    {
      "car_id": "2",
      "price": "500",
      "full_option": "700"
      "date_price": 04-05-2014
      ....
    },
    {
      "car_id": "2",
      "price": "900",
      "full_option": "1000"
      "date_price": 04-05-2013
      ....
    },
    ---more cars-------     
  ]
}

I would like to get differents arrays like :

if i consider variable "i" as number of different cars_id in my table

for(var i=1; i'<'numberOfDifferent_cars_id'<'i++){
    array[i]= datas based on same id
  }

My aim is to pare data of same object(cars_id) and making charts with price or date for example Thank you !

I have a nice puzzle : I have the following JavaScript array

var json = {
  'cars': [
    {
      "car_id": "1",
      "price": "925",
      "full_option": "1100"
      "date_price": 04-05-2016
      ...    
    }, 
    {
      "car_id": "1",
      "price": "990",
      "full_option": "1200"
      "date_price": 04-05-2015
      ....
    },
    {
      "car_id": "2",
      "price": "500",
      "full_option": "700"
      "date_price": 04-05-2014
      ....
    },
    {
      "car_id": "2",
      "price": "900",
      "full_option": "1000"
      "date_price": 04-05-2013
      ....
    },
    ---more cars-------     
  ]
}

I would like to get differents arrays like :

if i consider variable "i" as number of different cars_id in my table

for(var i=1; i'<'numberOfDifferent_cars_id'<'i++){
    array[i]= datas based on same id
  }

My aim is to pare data of same object(cars_id) and making charts with price or date for example Thank you !

Share Improve this question edited Feb 27, 2017 at 16:47 John asked May 4, 2016 at 9:07 JohnJohn 1951 gold badge6 silver badges23 bronze badges 2
  • "I have the following JavaScript JSON array" Just JavaScript, not JSON. JSON is a textual notation for data exchange. If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. – T.J. Crowder Commented May 4, 2016 at 9:12
  • When you were asking your question, there was a big orange How to Format box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a [?] button giving formatting help. And a preview area located between the text area and the Post Your Question button (so that you'd have to scan past it to find the button) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Commented May 4, 2016 at 9:13
Add a ment  | 

3 Answers 3

Reset to default 4

Also a nice and short solution:

cars = cars.filter((v, i, a) => a.findIndex(t => (t.card_id === v.id)) === i);

You can try something like this. Use the reduce function on the array, to populate a hashmap of the cars mapped by id. And the map over the keys, to get the array of all car items with the same id.

var json = {
  'cars': [{
    "car_id": "1",
    "price": "925",
    "full_option": "1100"
  }, {
    "car_id": "1",
    "price": "990",
    "full_option": "1200"
  }, {
    "car_id": "2",
    "price": "500",
    "full_option": "700"
  }, {
    "car_id": "2",
    "price": "900",
    "full_option": "1000"
  }, {
    "car_id": "4",
    "price": "900",
    "full_option": "1000"
  }]
};
console.log(json);

var car_array = json.cars.reduce((prev, t, index, arr) => {
  if (typeof prev[t.car_id] === 'undefined') {
    prev[t.car_id] = [];
  }
  prev[t.car_id].push(t);
  return prev;
}, {});



Object.keys(car_array).forEach(i => {
  var array_of_cars_with_same_id = car_array[i];
  //use this array to do some stuff
  console.log(array_of_cars_with_same_id);
});

Another solution

function removeDuplicates(myArr, prop) {
    return myArr.filter((obj, pos, arr) => {
        return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
    });
}

source

发布评论

评论列表(0)

  1. 暂无评论