How to inject Array with an object only if the object is not exist?

Viewed 104

I have my empty Array

locations = []

and I have another Array with locations and city's

Data = [
    { location: "01" , city: "01"},
    { location: "01" , city: "02"},
    { location: "03" , city: "03"},
    { location: "04" , city: "04"},
    { location: "01" , city: "01"},
    { location: "01" , city: "01"}
]

how can I loop through array Data[] and add the objects in locations[] without repeating it like first objects and last two objects

locations = [
    { location: "01" , city: "01"},
    { location: "01" , city: "02"},
    { location: "02" , city: "02"},
    { location: "03" , city: "03"},
    { location: "04" , city: "04"}
]
7 Answers

You can use Array.reduce() to filter out the duplicates and get a unique array of objects, locations:

var Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
];
var locations = Data.reduce((acc, obj)=>{
  var exist = acc.find(({location, city}) => obj.location === location && obj.city === city);
  if(!exist){
    acc.push(obj);
  }
  return acc;
},[]);
console.log(locations);

You can use simply forEach and push value with combination of location and city separated by "_"

var locations = {}

var Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
]

Data.forEach(val=>{ 
 var key = val.location+"_"+val.city;
 locations[key] = val;
 });
 
 locations = Object.values(locations);
 
 console.log(locations);

Could do the job

Data = [
  { location: "01", city: "01" },
  { location: "01", city: "02" },
  { location: "03", city: "03" },
  { location: "04", city: "04" },
  { location: "01", city: "01" },
  { location: "01", city: "01" }
]

const locations = Data.reduce( ( acc, d ) => {
  if ( !acc.some( ( a ) => a.city == d.city && a.location == d.location ) ) {
    acc.push( d )
  }
  return acc
}, [] )

console.log( locations )

You can use a combination of .filter() and .map() methods, like this:

var locations = Data.filter((d,i) => Data.map(dt => dt.location).indexOf(d.location) == i);

Demo:

Data = [
{ location: "01" , city: "01"},
{ location: "01" , city: "02"},
{ location: "03" , city: "03"},
{ location: "04" , city: "04"},
{ location: "01" , city: "01"},
{ location: "01" , city: "01"}
];

var locations = Data.filter((d,i) => Data.map(dt => dt.location).indexOf(d.location) == i);
console.log(locations);

function unique(a, key) {
            var seen = {};
            return a.filter(function (item) {
                item = key ? item[key] : item;
                return seen.hasOwnProperty(item) ? false : (seen[item] = true);
            });
        }
//var locations= unique(Data , "city");
var locations= unique(unique(Data , "city") , "location");

you can simply use Array.filter() and Set()(each entry of set a combination of city and location). If an element is not present in set than add it and add that element to set also. Try the following:

let Data = [ { location: "01" , city: "01"}, { location: "01" , city: "02"}, { location: "03" , city: "03"}, { location: "04" , city: "04"}, { location: "01" , city: "01"}, { location: "01" , city: "01"} ];
let set = new Set();

let result = Data.filter((e)=>{
   var key = e.location + "_" + e.city;
  if(!set.has(key)){
    set.add(key);
    return true;
  }
  return false;
});
console.log(result);

You can add the location name as key to to an object for uniqness check, and then return the object values once you completed the array iteration using Array.reduce.

var data = [{
    location: "01",
    city: "01"
  },
  {
    location: "01",
    city: "02"
  },
  {
    location: "03",
    city: "03"
  },
  {
    location: "04",
    city: "04"
  },
  {
    location: "01",
    city: "01"
  },
  {
    location: "01",
    city: "01"
  }
]

var result = data.reduce((mem, cur) => {
  var key = `{cur.location}--${cur.city}`
  if (!mem[key]) {
    mem[key] = cur;
  }
  return mem
}, {})

console.log(Object.values(result));

Related