last day (14 days later) » 

06:15
1
A: Detect duplicate key and replace its value within an array in javascript

TibrogarganThis should work function pushToAry(name, val) { for (var i = 0; i < ARY.length; i++) { if (ARY[i].hasOwnProperty(name)) { ARY[i][name] = val; return; } } var obj = {}; obj[name] = val; ...

let me try.....
giving compilation error Parse error. missing ; after for-loop initializer in the line for (let elem of ARY)
I believe you have a typo, the snippet works.
compilation error resolves after i change for (let elem of ARY) to for (var elem in ARY)
Interesting. What browser are you using?
I am doing this in a hybrid mobile app using kony studio
this code is not working :( ....still duplicate values are getting inserted
06:15
I guess it's not ECMA6
Let me modify it for use with pre ECMA 6 env
Trying..........
fINALLY WORKED...THANKS
You might want to consider revisiting your data structure. In more than a few instances it will cause you to write more code than you would need if you used a more native structure. (For instance, this function could almost be re-written as function pushToAry(name, val) { ARY[name] = val; } (If course, you would lose the ability to have duplicates - but that is fairly easily solved)
Yes surely I will have a look at it
Can you please help me with one more thing??
How to iterate through this array??and get the key and its value
Sure, but it's past 11pm so I have to go soon
06:16
You mean if you used ARY[name] = val?
I just wank to get the key name and their corresponding values.I have to set the key names in a lebel
You can use any of the Array iterators, like .map(), .filter() or .reduce()
Oh, Object.keys(ARY)
gives you an array of property names
You might do something like: Object.keys(ARY).map(function(name, index) { document.body.innerHTML.append(name+": "+ARY[name]+"</br>");});
ok, don't do that. body doesn't have an innerHTML, lol
man, I am tired. Don't try to use .map(), .filter() or .reduce() on an Object, they don't exist.
 
1 hour later…
07:44
Ohkk thanks a lot

last day (14 days later) »