# check if an object with a specific key value exists in an array of objects

• A+
Category：Languages

I'm trying to check if an array of items contains an object with a specific key value, and if it does, just update the array of items with the new item object

``[{name: example1, quantity: 2},{name: example2, quantity: 3},{name: example3, quantity: 5}] ``

so if for example I'm pushing an object with the same name of "example1", I want to check if it does exist in the array and if it does, just update it with the new object with the new quantity for example.

I tried to explain what I want as best as I can, please let me know if you need more clarification.

You can use a simple combination of `Array#some()` and `Array#find()` methods:

``if (data.some(o => o.name == toPush.name)) {   data.find(o => o.name == toPush.name).quantity = toPush.quantity; } ``

Where `data` is your `array` and `toPush` is the `object` you are trying to update.

Demo:

``var data = [{   name: "example1",   quantity: 2 }, {   name: "example2",   quantity: 3 }, {   name: "example3",   quantity: 5 }];  let toPush = {   name: "example2",   quantity: 10 };  if (data.some(o => o.name == toPush.name)) {   data.find(o => o.name == toPush.name).quantity = toPush.quantity; }  console.log(data);``
{"ft_lang_label":"__label__en","ft_lang_prob":0.6137385,"math_prob":0.81881326,"size":1149,"snap":"2019-26-2019-30","text_gpt3_token_len":310,"char_repetition_ratio":0.17641921,"word_repetition_ratio":0.13541667,"special_character_ratio":0.3002611,"punctuation_ratio":0.22352941,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9655871,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-21T08:37:37Z\",\"WARC-Record-ID\":\"<urn:uuid:f4cb0993-bb76-484c-bd0e-8f6cc50a9dfe>\",\"Content-Length\":\"34732\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:673467bd-6853-48ec-81cb-4e06f71db44d>\",\"WARC-Concurrent-To\":\"<urn:uuid:f4695356-4a01-45e6-95ce-2c376f75a6e2>\",\"WARC-IP-Address\":\"45.56.93.102\",\"WARC-Target-URI\":\"https://www.developerfaqs.com/4271/check-if-an-object-with-a-specific-key-value-exists-in-an-array-of-objects\",\"WARC-Payload-Digest\":\"sha1:A34NPORNQJSEJDTBE3ZN22HWVDFJBD2F\",\"WARC-Block-Digest\":\"sha1:ATAIBRPMIXTQIQZWUUAYBAPP2I6H6F5R\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195526940.0_warc_CC-MAIN-20190721082354-20190721104354-00299.warc.gz\"}"}