As in the array with different keys to send the data to the page?

Good evening!

Did not get to do to my data was displayed on the page from the array

<button>Button</button>
the <div></div>

$('button').click(function() {

 let button = $(this).next();

 let arr = [
 'nameOneItem' : 'Name one',
 'nameTwoItem' : 'Name two'
];

 let out = "; 

 for (let i = 0; i < arr.length; i++ ) {
 out += '<span>' + arr[i] + '</span>';
}

button.append(out);
});


Here is a more clear example: https://codepen.io/Nels.Co/pen/oRJdoW
March 23rd 20 at 18:41
3 answers
March 23rd 20 at 18:43
Solution
$('button').click(function() {

 let button = $(this).next();

 let arr = {
 'nameOneItem' : 'Name one',
 'nameTwoItem' : 'Name two'
};

 let out = "; 

 for (let i in arr) {
 out += '<span>' + arr[i] + '</span>';
}

button.append(out);
});
Ie javascript can work with object keys and sort through different keys in arrays (like in PHP) can't he ? - Nels.Co commented on March 23rd 20 at 18:46
@Nels.Co, in js an array is an object with integer keys instead of string - Candida95 commented on March 23rd 20 at 18:49
Javascript never PHP. He even does not guarantee the order of keys (in General). JavaScript is syntactically separate sets: 1) classical arrays are indexed with square brackets [ ] 2) objects with any properties curly braces { }
In JS all sorts of wonderful situations, so better not to mix different approaches. - axel commented on March 23rd 20 at 18:52
@Candida95, but what's the catch

Simply in php you can use string as keys - Nels.Co commented on March 23rd 20 at 18:55
March 23rd 20 at 18:45
Solution
You have spelled incorrectly......... so do not record elements in the array.
Here is the link how

and code just in
let obj = {
 'nameOneItem' : 'Name one',
 'nameTwoItem' : 'Name two'
};

 $('button').click(function() {
 for(let key in arr){
console.log(arr[key])
}
 });
March 23rd 20 at 18:47
Solution
let arr = [
 'nameOneItem' : 'Name one',
 'nameTwoItem' : 'Name two'
];

why use an array as an object?

or, use the array normally
let arr = [
 'Name one',
 'Name two'
];

an object
let obj = {
 'nameOneItem' : 'Name one',
 'nameTwoItem' : 'Name two'
};

let out = "; 
let arr = Object.values(obj);

for (let i = 0; i < arr.length; i++ ) {
 out += '<span>' + arr[i] + '</span>';
}
In this case, we have not added the keys to the array ?

https://codepen.io/Nels.Co/pen/EzGRPX

0: "[id:a]"
1: "[id:b]"
2: "[id:c]"
3: "[id:d]"
- Nels.Co commented on March 23rd 20 at 18:50
@Nels.Cohas not added - Candida95 commented on March 23rd 20 at 18:53
@Candida95, thanks for the reply!

In the console I guess (not added), but wanted to know others opinion on this matter) - Nels.Co commented on March 23rd 20 at 18:56

Find more questions by tags HTMLjQueryJavaScript