简体   繁体   中英

Javascript : Function to return distinct values in a 2D array

I have an array to

var table = [
      {
        country:"india",
        b:2
      },
      {
         country:"usa",
        b:33
      },
       {
         country:"australia",
        b:3
      },
       {
         country:"india",
        b:32
      },
       {
         country:"southafrica",
        b:31
      },
       {
         country:"australia",
        b:30
      },
      {
        country:"india",
        b:40
      }
    ];

result expected :

 var table = [
      {
        country:"india",
        b:2
      },
      {
         country:"usa",
        b:33
      },
       {
         country:"australia",
        b:3
      }, 
       {
         country:"southafrica",
        b:31
      }

    ];

My code is :

function getUniqueValuesOfKey(array, key){
  return array.reduce(function(carry, item){
    if(item[key] && !~carry.indexOf(item[key])) carry.push(item[key]);
    return carry;
  }, []);
}

document.write(JSON.stringify(getUniqueValuesOfKey(table, 'a')));

how to get the unique array result based on the country key

Try this ,With Array reduce and Filter.Hope It helps

 var table = [{ country: "india", b: 2 }, { country: "usa", b: 33 }, { country: "australia", b: 3 }, { country: "india", b: 32 }, { country: "southafrica", b: 31 }, { country: "australia", b: 30 }, { country: "india", b: 40 }]; let result = table.reduce((acc, ele) => { if (acc.filter(el => el.country == ele.country).length == 0) { acc.push(ele) } return acc; }, []) console.log(result)

You can use .filter() to get the filtered array:

 const data = [ { country:"india", b:2 }, { country:"usa", b:33 }, { country:"australia", b:3 }, { country:"india", b:32 }, { country:"southafrica", b:31 }, { country:"australia", b:30 }, { country:"india", b:40 } ]; const getUniqueData = (arr, names = []) => arr.filter(({ country }) => ( names.includes(country) ? false : (names.push(country), true) )); console.log(getUniqueData(data));
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Simple solution with o(n) time complexity.

var table = [{
  country: "india",
  b: 2
}, {
  country: "usa",
  b: 33
}, {
  country: "australia",
  b: 3
}, {
  country: "india",
  b: 32
}, {
  country: "southafrica",
  b: 31
}, {
  country: "australia",
  b: 30
}, {
  country: "india",
  b: 40
}];

let result = [];
const countries = {};

for(let item of table) {
    let {country, b} = item;
    if(!countries[country]) {
        countries[country] = b;
        result.push(item);
    }
}

console.log(result);

You can simply achieve this with one for loop.

 const table = [{ country: 'india', b: 2 }, { country: 'usa', b: 33 }, { country: 'australia', b: 3 }, { country: 'india', b: 32 }, { country: 'southafrica', b: 31 }, { country: 'australia', b: 30 }, { country: 'india', b: 40 }]; const getUniqueArray = (array, tempObj = {}) => { const uniqueArray = []; for (let i = 0; i < array.length; i += 1) { const element = array[i]; const { country, b } = element; if (!tempObj[country]) { tempObj[country] = b; uniqueArray.push(element); } } return uniqueArray; }; const result = getUniqueArray(table); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM