var str=prompt("Enter the string;").split(" ");
var lent=str.map(function(x){
return {
x : x.length
};
});
document.write("<br>The length is 1 word is :"+lent[0][str[0]]);
So say you have a string `“Hello I am a string”, your map produces an array like:
[{ x: 5}, {x: 1}, {x: 2}, {x: 1}, {x: 6}]
You then pick the first item (lent[0]
), and then try to access a sub-index that doesn’t exist ([str[0]]
).
then how to access the value of the key…please tell me.
For some reason you’ve created an object: {x: length}
where length is the length of the word. You just access it like any other object, it has a key that you’ve called x
. Like lent[0]
is {x: 5}
in my example, it’s literally that.
lent[0].x or lent[0][“x”] is the way to access