I am having trouble applying objects to my array, every time i push the object into the array a new array is created.
I have made some edits at the bottom of the question.
I need an array of objects to look like this
var arr = [{name: 'bent', town: 'kansas'}, { name:'benny', town:'vegas'}];
Insted mine looks like this
var arr = [0:{name: 'bent', town: 'kansas'}1:{ name:'benny', town:'vegas'}]
PHP
$stmt = $dbCon->prepare("SELECT ctc_coins_name, "
. " ctc_coins_town "
. " FROM ctc_coins ");
$stmt->execute();
$result_coins = $stmt->fetchAll();
$coins = [];
foreach ($result_coins as $result) {
$coins[] = [
'name' => $result['ctc_coins_name'],
'twon' => $result['ctc_coins_town]
];
}
echo json_encode($coins);
Javascript
function update_local_coins() {
var http = new XMLHttpRequest();
var url = "pages/ajax/getCoins.php";
http.open("POST", url, true);
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
if (this.responseText) {
jsonObj = JSON.parse(this.responseText);
console.log(jsonObj);
var coins = [];
for (var l in jsonObj) {
coins.push(jsonObj[l]);
}
console.log("COINS");
console.log(coins);
} else {
alert("Noobs");
}
}
};
http.send();
}
I have tried this with no luck
coins.concat(jsonObj[l]) 'returns empty array'
coins.push.apply(coins, jsonObj[l]) 'returns empty array'
coins.push(jsonObj[l]) 'returns the example array'
EDIT:
console.log(jsonObj);
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{name: "Bitcoin", usd: "5622.73", btc: "1.00000000"}
1
:
{name: "Ethereum", usd: "338.81", btc: "0.06013390"}
2
:
{name: "Litecoin", usd: "58.65", btc: "0.01040990"}
3
:
{name: "Dash", usd: "307.31", btc: "0.05454360"}
4
:
{name: "Golem", usd: "0.21", btc: "0.00003759"}
5
:
{name: "NEM", usd: "0.21", btc: "0.00003674"}
6
:
{name: "Neutron", usd: "0.15", btc: "0.00002686"}
7
:
{name: "DigiByte", usd: "0.01", btc: "0.00000175"}
8
:
{name: "Siacoin", usd: "0.00", btc: "0.00000074"}
9
:
{name: "Linda", usd: "0.00", btc: "0.00000005"}
10
:
{name: "LiteDoge", usd: "0.00", btc: "0.00000001"}
11
:
{name: "ArcticCoin", usd: "0.05", btc: "0.00000922"}
12
:
{name: "NoLimitCoin", usd: "0.13", btc: "0.00002321"}
length
:
13
__proto__
:
Array(0)
EDIT: php echo json
[{"name":"Bitcoin","usd":"5622.73","btc":"1.00000000"},{"name":"Ethereum","usd":"338.81","btc":"0.06013390"},{"name":"Litecoin","usd":"58.65","btc":"0.01040990"},{"name":"Dash","usd":"307.31","btc":"0.05454360"},{"name":"Golem","usd":"0.21","btc":"0.00003759"},{"name":"NEM","usd":"0.21","btc":"0.00003674"},{"name":"Neutron","usd":"0.15","btc":"0.00002686"},{"name":"DigiByte","usd":"0.01","btc":"0.00000175"},{"name":"Siacoin","usd":"0.00","btc":"0.00000074"},{"name":"Linda","usd":"0.00","btc":"0.00000005"},{"name":"LiteDoge","usd":"0.00","btc":"0.00000001"},{"name":"ArcticCoin","usd":"0.05","btc":"0.00000922"},{"name":"NoLimitCoin","usd":"0.13","btc":"0.00002321"}]
Your objective was to get the array of objects received in the AJAX response.
As I said in my first comment to your question, you already got this in this line
jsonObj = JSON.parse(this.responseText);
Now, you seem to think that you have received an object(with numbers being fields, and the objects being values). But this is just how console.log behaves :)
As for your final question regarding a way to confirm that is indeed an array, all you need to do is check the string representation of the result of JSON.parse()
i.e use JSON.stringify
. Below example demonstrates this.
EDIT: Have updated code snippet with demonstration of findMatches()
posted in the comment.
var s = '[{"name":"Bitcoin","usd":"5622.73","btc":"1.00000000"},{"name":"Ethereum","usd":"338.81","btc":"0.06013390"},{"name":"Litecoin","usd":"58.65","btc":"0.01040990"},{"name":"Dash","usd":"307.31","btc":"0.05454360"},{"name":"Golem","usd":"0.21","btc":"0.00003759"},{"name":"NEM","usd":"0.21","btc":"0.00003674"},{"name":"Neutron","usd":"0.15","btc":"0.00002686"},{"name":"DigiByte","usd":"0.01","btc":"0.00000175"},{"name":"Siacoin","usd":"0.00","btc":"0.00000074"},{"name":"Linda","usd":"0.00","btc":"0.00000005"},{"name":"LiteDoge","usd":"0.00","btc":"0.00000001"},{"name":"ArcticCoin","usd":"0.05","btc":"0.00000922"},{"name":"NoLimitCoin","usd":"0.13","btc":"0.00002321"}]';
var arrayOfObjects = JSON.parse(s);
var stringifiedArray = JSON.stringify(arrayOfObjects);
document.getElementById("op").innerText = stringifiedArray;
function findMatches(arr, searchString) {
const matches = [];
arr.forEach(obj => {
for(const key in obj) {
if (obj[key].includes(searchString)) {
matches.push(obj[key]);
}
}
});
return matches;
}
var matches = findMatches(arrayOfObjects, "Lite");
document.getElementById("mr").innerText = JSON.stringify(matches);
<p id="op">
<!-- Result of JSON.stringify will appear here -->
</p>
<hr>
<p id="mr">
<!-- Result of findMatches will appear here -->
</p>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加