I am looping through an array and checking if a second array has a key by that name. If not then add this current name as a key to second array. I check with hasOwnProperty and also with in but in both cases the key is getting added in second array though a key by that name is already present.How can stop a key getting added if it is already present?
function(){
var _arraySource = ['Tea', 'Coffe', 'Banana', 'Orange', 'Tea'];
var _jsonArray = [];
for(var i = 0, j = _arraySource.length; i<j; i++){
if(_jsonArray.hasOwnProperty(_arraySource[i])){
//Do nothing
}
else{
var _key =_arraySource[i];
var myObj = {};
myObj[_key] = "";
_jsonArray.push(myObj);
}
}
console.log(_jsonArray);
}