2012-02-07 9 views
11
var arr = [{ 
    key: "key1", value: "z" 
}, { 
    key: "key2", value: "u" 
}, { 
    ... 
}]; 

कि मेरे key:"key1" पहले से ही मौजूद है या नहीं कैसे जाँच करने के लिए। यदि यह अस्तित्व में नहीं है, तो मुझे एम सरणी में कुंजी जोड़ने की आवश्यकता है।कैसे जाँच करने के लिए कि मेरे कुंजी सरणी वस्तु में मौजूद है

var map = { 
     "key1": "z", 
     "key2": "u" 
}; 

तो फिर आप अपने जांच कर सकता है और यदि अपनी चाबी वस्तु पर किसी भी मौजूदा गुण के साथ संघर्ष नहीं है और आप डॉन ':

if(arr.hasOwnProperty("key1")){ 
     arr.unshift({key:"key1", value:"z"}); 
} 

उत्तर

7

आप वस्तुओं से भरा एक सरणी मिल गया है के बाद से, आप इसे करने की जरूरत है की तरह सरणी से अगर वे आपका परीक्षण पास नहीं करते हैं। जिसके परिणामस्वरूप सरणी कोई तत्व है, तो आपको पता है कि अपने मूल्य के साथ कोई भी थे कि:

if(!arr.filter(function(elem) { 
    return elem.key === "key1"; 
}).length) { 
    arr.push({ key: "key1", value: "z" }); 
} 

आप इसे पुराने ब्राउज़र क्या आप वाकई Array.prototype.filter परिभाषित किया गया है बनाने के लिए एक परत का उपयोग करने की आवश्यकता होगी में काम करना चाहते हैं।

18

है ताकि आप अपने डेटा thusly संग्रहीत करना चाहिए बनाने के लिए शून्य मूल्यों की आवश्यकता नहीं है आप इसे आसान बना सकते हैं।

if (!map["key1"]) { 
    map["key1"] = "z"; 
} 

तुम सच में पूर्ण वस्तु की जरूरत है (तुम्हारा सब के बाद सिर्फ एक उदाहरण है), मैं, कुंजी के मूल्य के रूप में वस्तु की दुकान नहीं बस सरणी में वस्तुओं संग्रहीत करेंगे। यही है, इसे एक नक्शा बनाओ, एक सरणी नहीं।

function lookup(name) { 
    for(var i = 0, len = arr.length; i < len; i++) { 
     if(arr[ i ].key === name) 
      return true; 
    } 
    return false; 
} 

if(!lookup('key1')) { 
    arr.push({ 
     key: 'key1', 
     value: 'z' 
    }); 
} 
1
var key; 
for(var i = 0; i < arr.length; i++) 
{ 
    if(arr[i].key == "key1") 
    { 
     key = arr[i]; 
     break; 
    } 
} 
if(typeof (key)=='undefined') //for if the value is 0 as int 
{ 
    key = { 
     key: "key1", value: "aaa" 
    }; 
    arr.push(key); 
} 
5

आप इस्तेमाल कर सकते हैं ECMAScript 5 filter विधि तत्वों को दूर करने के

(ES3):

1

आप एरे कुंजी या ऑब्जेक्ट प्रॉपर्टी मौजूद हैं या नहीं, यह देखने के लिए आप दोनों सरणी और ऑब्जेक्ट्स देख सकते हैं। यह बहुत उपयोगी है, और यह दोनों प्रकार की जांच के लिए उसी तरह प्रयोग किया जाता है।

/** 
* Check if an array key or object property exists 
* @key - what value to check for 
* @search - an array or object to check in 
*/ 
function key_exists(key, search) { 
    if (!search || (search.constructor !== Array && search.constructor !== Object)) { 
     return false; 
    } 
    for (var i = 0; i < search.length; i++) { 
     if (search[i] === key) { 
      return true; 
     } 
    } 
    return key in search; 
} 

उपयोग:

एक सरणी

key_exists('jared', ['jared', 'williams']); //= true 

एक वस्तु के रूप में के रूप में

key_exists('jared', {'jared': 'williams'}); //= true 
0

नीचे दो, अधिक स्पष्ट, versi हैं @ जे एंडी के स्वीकृत उत्तर के ऑन्स।

मैं खुद के लिए पहले संस्करण बनाया तो मैं तर्क बेहतर समझ सकते हैं और निम्नलिखित कहा:

यदि कुंजी मौजूद है, मिलान वस्तु की गिनती संपत्ति को बढ़ा देते अन्यथा एक नई वस्तु बनाने 1.

दूसरे संस्करण में की गणना के साथ, मुझे एहसास हुआ कि मैं अपने arrayOfObjects चर पसंद करेंगे ताकि बाद में मैं विशेष रूप से नहीं बल्कि सरणी पर पाशन जब तक मैं एक मैच मिल की तुलना में मूल्यों को लक्ष्य बना सकते एक object होने के लिए,, और फिर releva हो रही है एनटी वस्तु मूल्य।तो संस्करण ऑब्जेक्ट्स की सरणी के बजाय ऑब्जेक्ट का उपयोग करता है।

संस्करण 01 - वस्तुओं

// based on: https://stackoverflow.com/a/9177103/1063287 
 

 
// the original array of objects 
 
var arrayofObjects = [{ 
 
    id: "CY01", 
 
    count: 1 
 
    }, 
 
    { 
 
    id: "CY33", 
 
    count: 5 
 
    }, 
 
    { 
 
    id: "CY55", 
 
    count: 8 
 
    } 
 
]; 
 

 
// show the array in the interface 
 
$(".before").text(JSON.stringify(arrayofObjects)); 
 

 
// define lookup function (must have access to arrayofObjects) 
 
function lookup(key_to_check) { 
 
    // for each object in the array of objects 
 
    for (var i = 0; i < arrayofObjects.length; i++) { 
 
    // if the object key matches the key to check 
 
    if (arrayofObjects[i]["id"] === key_to_check) { 
 
     // return true with index of matching object 
 
     var returnObject = {}; 
 
     returnObject["exists"] = true; 
 
     returnObject["index"] = i; 
 
     return returnObject; 
 
    } 
 
    } 
 
    // if the above loop has not already returned a value 
 
    // return false 
 
    var returnObject = {}; 
 
    returnObject["exists"] = false; 
 
    return returnObject; 
 
} 
 

 
// on click, check whether the key exists 
 
$(document).on("click", ".run", function() { 
 

 
    var key_to_check = $(".key_to_check").val(); 
 

 
    $(".checking").text(key_to_check); 
 

 
    var returnObject = lookup(key_to_check); 
 

 
    // if key to check doesn't exist add it 
 
    if (returnObject["exists"] === false) { 
 
    console.log("key doesn't exist, adding object"); 
 
    arrayofObjects.push({ 
 
     id: key_to_check, 
 
     count: 1 
 
    }); 
 
    } else if (returnObject["exists"] === true) { 
 
    // else if it does exists, increment the relevant counter 
 
    console.log("key does exist, incrementing object count value"); 
 
    var index = returnObject.index; 
 
    arrayofObjects[index].count += 1; 
 
    } 
 

 
    $(".after").text(JSON.stringify(arrayofObjects)); 
 
});
body { 
 
    font-family: arial; 
 
    font-size: 14px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>enter an existing or non-existing key and click run.</p> 
 
<p>if existing, increment count, otherwise create new object with count of 1.</p> 
 

 
<input class="key_to_check"><button class="run">run</button> 
 

 
<br><br> 
 
<div>array of objects - before: <span class="before"></span> </div> 
 

 
<div>checking:<span class="checking"></span></div> 
 

 
<div>array of objects - after: <span class="after"></span></div>

संस्करण 02 की एक सरणी - एक वस्तु

// based on: https://stackoverflow.com/a/9177103/1063287 
 

 
// the original object 
 
var myObject = { 
 
    "CY01": 1, 
 
    "CY33": 5, 
 
    "CY55": 8 
 
}; 
 

 
// show the object in the interface 
 
$(".before").text(JSON.stringify(myObject)); 
 

 
// define lookup function (must have access to myObject) 
 
function lookup(key_to_check) { 
 
    // for each property in the object 
 
    for (key in myObject) { 
 
    // if the key matches the key to check 
 
    if (key === key_to_check) { 
 
     // return true 
 
     return true 
 
    } 
 
    } 
 
    // if the above loop has not already returned a value 
 
    // return false 
 
    return false 
 
} 
 

 
// on click, check whether the key exists 
 
$(document).on("click", ".run", function() { 
 

 
    var key_to_check = $(".key_to_check").val(); 
 

 
    $(".checking").text(key_to_check); 
 

 
    var returnObject = lookup(key_to_check); 
 

 
    // if key to check doesn't exist add it 
 
    if (returnObject === false) { 
 
    console.log("key doesn't exist, adding object"); 
 
    myObject[key_to_check] = 1; 
 
    } else if (returnObject === true) { 
 
    // else if it does exists, increment the relevant counter 
 
    console.log("key does exist, incrementing object count value"); 
 
    myObject[key_to_check] += 1; 
 
    } 
 

 
    $(".after").text(JSON.stringify(myObject)); 
 
});
body { 
 
    font-family: arial; 
 
    font-size: 14px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p>enter an existing or non-existing key and click run.</p> 
 
<p>if existing, increment count, otherwise create new property with count of 1.</p> 
 

 
<input class="key_to_check"><button class="run">run</button> 
 

 
<br><br> 
 
<div>my object - before: <span class="before"></span> </div> 
 

 
<div>checking:<span class="checking"></span></div> 
 

 
<div>my object - after: <span class="after"></span></div>

संबंधित मुद्दे