How to add to an array in javascript? [duplicate] - javascript

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 6 years ago.
is anyone able to tell me what i am doing wrong? The problem is that "ramdom1" is not being added to the array.(nothing is showing up in the text_loot)
Any help would be great, thanks.
var lootArray = [].join("<br>");
lootArray.add("ramdom1");
document.getElementById("text_loot").innerHTML = lootArray;

First, the method to add element to array is called push, not add
Second, when you do join on array, you get string, which doesn't have methods like push or add
If you really want to do this, you need this code:
var lootArray = []
lootArray.push("ramdom1");
lootArray = lootArray.join("<br>");
document.getElementById("text_loot").innerHTML = lootArray;

Replace
lootArray.add("ramdom1");
with
lootArray.push("ramdom1");

use push method like this. hope it will works
lootArray.push("ramdom1");

Related

Parse value only from JSON string [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);

.next('tag') in plain javascript? [duplicate]

This question already has answers here:
Get next element with specific class of clicked element with pure js
(3 answers)
Closed 4 years ago.
Is it possible to search for the next sibling-element with a specified tag?
.nextElementSibling is not a function so... can't pass any tag with it?
jQuery
$(event.target).parent().next('div').attr('id')
Java Script
event.target.parentElement.nextElementSibling.getAttribute('id');
you should try using querySelectorAll https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
and then simply just looping over the results for your desired result. Please have a look at the following js fiddle
https://jsfiddle.net/c0bdgxtj/12/
var wrapper = document.getElementById("wrapper");
var debugEle = document.getElementById("debug");
var desiredElements = wrapper.querySelectorAll('div[data-test="y"]');

How to combine multiple find() in jquery? [duplicate]

This question already has answers here:
jquery multiple attribute selector problem
(3 answers)
Closed 6 years ago.
I have these two conditions:
1) Find all div with a "data-filter-program" attribute which equal to something
2) Find all div with a "data-filter-expertise" attribute which equal to something
And I need to somehow combine these two into one statement in jquery. I would like something like this:
mentors = $("div").find("[data-filter-program*='"+selected_program+"']" && "[data-filter-expertise*='"+selected_expertise+"']");
How do I actually achieve this correctly in Jquery?
Thanks!
mentors = $("div").find("[data-filter-program*='"+selected_program+"'], [data-filter-expertise*='"+selected_expertise+"']");
Just add a comma between each item you would like to find.
$("div").find("#apples, #bananas, .grapes, div");

print/alert array in javascript [duplicate]

This question already has answers here:
How can I display a JavaScript object?
(40 answers)
Closed 7 years ago.
I have an array in javascript and I want to print/alert it (without change array).
my code is like this:
function myFunction() {
var fruits = [{'a':"Banana", 'c':"Orange", 'v':"Apple", 's':"Mango"}];
fruits[0].toString();
document.getElementById("demo").innerHTML = fruits;}
can anybody help me please?
You can simply use JSON.stringify:
JSON.stringify(fruits);
jsfiddle

How can I change the text using JavaScript [duplicate]

This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 7 years ago.
<div class="basePrice">$99.99</div>
I want to change the value $99.99 using JavaScript.
I tried using getElementByClass but it didn't produce the results that I was hoping to get.
document.getElementsByClass returns a NodeList, which is kind of like an array.
You have to specify which element (there's only one here, so I'm assuming the first) you're looking for, and then you can use .textContent to change the text of the node.
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
document.getElementsByClassName("basePrice")[0].textContent = "$49.99";
<div class="basePrice">$99.99</div>
You can do that like so
document.querySelector('.basePrice').innerHTML = 'different text'
FIDDLE
try getElementByClassName I believe is the proper syntax.

Categories