1

Let's say I have an array of objects like this:

[
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

And I want to filter through it and create new objects based on the model field.

With the end result being:

[
  { "type": 121, "model": "model1" }
]

[
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

I'm using typescript and lodash so anything with that would be best

I tried lodash groupBy and ES6 mapping, but no success so far. I guess I could do it in a dirty way with multiple forEach loops but I'm pretty sure there is a much easier way.

1
  • can you please provide your code or https://stackblitz.com/? Commented Jan 4, 2019 at 10:39

6 Answers 6

2

You can use .filter, like this:

let newarray1 = array.filter(obj => obj.model === 'model1');
let newarray2 = array.filter(obj => obj.model === 'model2');
...
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.

var array = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
];


var searchModels = ['model1', 'model3'];
var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
console.log("Original: " , array);
console.log("Filtered: " , filteredArray);

Comments

1

You could so something like that for example :) In this case I am creating a Map, I think it's easier for search

var newData = new Map([]);

var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
var formattedData = originalData.map(obj => { 
  newData[obj.model].push(obj.type); 
});

You will get something like :

newData = ([
    [ "model1", "130" ],
    [ "model1", "128" ],
    [ "model2", "3" ]
]);

And you can get any values using the model key newData["model2"] = ["3"]

Comments

1

you can use Array.prototype.reduce method to group all.

let array = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

let all = [...array.reduce((acc, curr) => {
  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
  return acc;
}, new Map()).values()];
console.log(...all)

Comments

1

try this @raulicious,

var arrayList= [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 131, "model": "model2" }
];


   var filteredArray = [];
   var filtered = [];
   
   arrayList.sort((a, b) => {
         if(a.model == b.model) {
             filtered.push(b);
         } else {
             filtered.push(b);
             filteredArray.push(filtered);
             filtered = [];
           }
       filtered.push(a);
       filteredArray.push(filtered);
     });
    console.log(filteredArray);

I know this has some redundant code, I am trying to reduce that soon

Comments

0

See, if this help with lodash

var data = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
];

var grouped = _.groupBy(data, function(item) {
  return item.model;
});

console.log(grouped);
<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>

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.