2

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

1
  • 1
    your input has no "a" keys Commented Aug 29, 2019 at 13:36

4 Answers 4

2

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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; }

Comments

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);

Comments

0

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); 

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.