最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Find Duplicate value From Collection Using Lodash - Stack Overflow

matteradmin7PV0评论

Using Lodash I am trying to find if a value exists in the collection or not.

If exists I want to return true else false.

 const d = [{
     "country": "India",
     "_id": ObjectId("5ad47b639048dd2367e95d48"),
     "cities": []
 }, {
     "country": "Spain",
     "_id": ObjectId("5ad47b639048dd2367e95d49"),
     "cities": []
 }];

Snippet Code

Countries = ['India', 'Spain']
if (_.has(d, Countries)) {
    console.log(true);
} else {
    console.log(false);
}

But it Always returns False. It's not remended for me to use lodash can anyone suggests a better way if possible.

Using Lodash I am trying to find if a value exists in the collection or not.

If exists I want to return true else false.

 const d = [{
     "country": "India",
     "_id": ObjectId("5ad47b639048dd2367e95d48"),
     "cities": []
 }, {
     "country": "Spain",
     "_id": ObjectId("5ad47b639048dd2367e95d49"),
     "cities": []
 }];

Snippet Code

Countries = ['India', 'Spain']
if (_.has(d, Countries)) {
    console.log(true);
} else {
    console.log(false);
}

But it Always returns False. It's not remended for me to use lodash can anyone suggests a better way if possible.

Share Improve this question edited Apr 16, 2018 at 12:26 Narendra Jadhav 10.3k15 gold badges35 silver badges44 bronze badges asked Apr 16, 2018 at 11:56 Nikhil SavaliyaNikhil Savaliya 2,1664 gold badges26 silver badges45 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

You can use the bination of some and includes methods. It will return true, if any item in items contains country from countries array.

const items =  [
    {
        "country": "India",
        "_id": 'ObjectId("5ad47b639048dd2367e95d48")',
        "cities": []
    },
    {
        "country": "Spain",
        "_id": 'ObjectId("5ad47b639048dd2367e95d49")',
        "cities": []
    }
];

const countries = ['India', 'Spain'];
const includes = _.some(items, item => _.includes(countries , item.country));
console.log(includes);
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>

ES6

You could use array.some() and array.includes() for checking duplicate entries in array.

DEMO

const d = [{
  "country": "India",
  "_id": "5ad47b639048dd2367e95d48",
  "cities": []
}, {
  "country": "Spain",
  "_id": "5ad47b639048dd2367e95d49",
  "cities": []
}];

const Countries = ['India', 'Spain'];

console.log(d.some(({country}) => Countries.includes(country)))
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can also do this without Lodash using Javascripts built in filter function

d.filter(item => item.country === "Spain"); //Would return an array of objects where the country's name property is Spain!

if we wanted to make this a boolean we could declare it as a variable and assert its length is greater than 0 like so:

let isSpanishCountry = d.filter(item => item.country === "Spain").length > 0; // console.log(isSpanishCountry) => true

Inspired by @Christian Bartram

var cities = [
  {city: "Beijing",bOn:false},
  {city: "Shanghai",bOn:false},
  {city: "Chongqing",bOn:false},
  {city: "Guangzhou",bOn:false}, {city: "Guangzhou",bOn:true},
  {city: "Xi'an",bOn:false}
];

//Find duplicate element values in the array
_(cities).groupBy(x => x.city).pickBy(x => x.length > 1).keys().value()

Return

['Guangzhou']

//Find duplicate objects in an array
_.filter(_(cities).groupBy(x => x.city).value(),x => x.length > 1)

Return

[[{"city":"Guangzhou","bOn":false},{"city":"Guangzhou","bOn":true}]]

Post a comment

comment list (0)

  1. No comments so far