There is an array of JavaScript objects. It is necessary to extract from this array only those objects that have unique values ​​by the VALUE key.

That is, from this source array:

let citiesArray = [{ VALUE: "Омск", ID: 1, CODE: "CITY" }, { VALUE: "Астана", ID: 2, CODE: "CITY" }, { VALUE: "Астана", ID: 22, CODE: "CITY" }, { VALUE: "Казань", ID: 3441, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 3, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 33, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 333, CODE: "CITY" }, { VALUE: "Омск", ID: 4, CODE: "CITY" }] 

should get the following:

 [{ VALUE: "Омск", ID: 1, CODE: "CITY" }, { VALUE: "Астана", ID: 2, CODE: "CITY" }, { VALUE: "Казань", ID: 3441, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 3, CODE: "CITY" }] 

The question is how to do it beautifully on ES6?

Closed due to the fact that off-topic participants pavel , aleksandr barakin , Kirill Stoianov , tutankhamun , Dmitriy Simushev 10 Oct '16 at 10:37 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - pavel, aleksandr barakin, Kirill Stoianov, Dmitriy Simushev
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    What is the question? remove. Or demonstrate what you have already tried to do and what exactly did not work out for you. - Vyacheslav Danshin

2 answers 2

 let tmpArray = []; function itemCheck(item) { if (tmpArray.indexOf(item.VALUE) === -1) { tmpArray.push(item.VALUE); return true } return false; } console.log(citiesArray.filter((item) => itemCheck(item))); 

    We need the function reduce and an object in which we will mark the value already or not:

     let citiesArray = [{ VALUE: "Омск", ID: 1, CODE: "CITY" }, { VALUE: "Астана", ID: 2, CODE: "CITY" }, { VALUE: "Астана", ID: 22, CODE: "CITY" }, { VALUE: "Казань", ID: 3441, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 3, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 33, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 333, CODE: "CITY" }, { VALUE: "Омск", ID: 4, CODE: "CITY" }] let cities = citiesArray.reduce((acc, city) => acc.map[city.VALUE] ? acc : ((acc.map[city.VALUE] = true), acc.cities.push(city), acc), { map: {}, cities: [] }).cities; console.log(cities); 

    In a more readable form:

     let citiesArray = [{ VALUE: "Омск", ID: 1, CODE: "CITY" }, { VALUE: "Астана", ID: 2, CODE: "CITY" }, { VALUE: "Астана", ID: 22, CODE: "CITY" }, { VALUE: "Казань", ID: 3441, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 3, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 33, CODE: "CITY" }, { VALUE: "Курган-Тюбе", ID: 333, CODE: "CITY" }, { VALUE: "Омск", ID: 4, CODE: "CITY" }] let cities = citiesArray.reduce((acc, city) => { if (acc.map[city.VALUE]) // если данный город уже был return acc; // ничего не делаем, возвращаем уже собранное acc.map[city.VALUE] = true; // помечаем город, как обработанный acc.cities.push(city); // добавляем объект в массив городов return acc; // возвращаем собранное }, { map: {}, // здесь будут отмечаться обработанные города cities: [] // здесь конечный массив уникальных городов }) .cities; // получаем конечный массив console.log(cities); 

    • This stuffing from small letters, parentheses and punctuation marks has nothing to do with beauty or readability. From me a minus for the machine-oriented code. - Dmitriy Simushev
    • @DmitriySimushev, is that better? - Grundy
    • Yes, at least you can read it :) - Dmitriy Simushev
    • @DmitriySimushev, I don’t get something beautiful, I wanted to do it through Set, and then there were two passes almost - Grundy