This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 5 years ago.
I'm trying to JSON.stringify an associatif array in javascript, when i console.log it the output is :
Array[0]
adresse:
"zfzf"
adresse2:
"zfz"
adresse3:
"fzfz"
code_postal
"fzfz"
[...]
length:
0
[...]
__proto__:
Array[0]
as you can see the lenght is equal to 0, so when i JSON.Strigify it, the answer is [ ]
i use to build my array like this :
var info = [];
$("[data-change]").each(function(key,obj){
if ($(obj).val() == "") {
i = 1;
}
else {
info[$(obj).attr("name")] = $(obj).val();
j++;
}
});
all those "data-change" are inputs, i've tryied to use the variable j instead of $(obj).attr("name")] and it works but i neeed the key to be the name of the input
Because Objects doesn't have length property.
Use Object.keys(obj).length to view length
Related
This question already has answers here:
Why does Array.prototype.push return the new length instead of something more useful?
(6 answers)
Closed 1 year ago.
I am trying to add products to the cart in my website with localStorage, so I initialize an array and then push products into it. The problem is when I use JSON.parse() to push a product and then stringify it again with JSON.stringify() it returns a number instead of the array. This is the part of my code that throws an error:
let product = {
id: i + 1,
name: productNames[i].innerText,
price: prices[i].innerHTML,
img: imgs[i].src
};
if(localStorage.productsInCart) {
let productsInLocalStorage = JSON.parse(localStorage.getItem("productsInCart") || "[]");
console.log(productsInLocalStorage);
let newArray = productsInLocalStorage.push(product);
console.log(JSON.stringify(newArray));
localStorage.productsInCart = JSON.stringify(newArray);
numberOfProducts.innerHTML = parseInt(numberOfProducts.innerHTML, 10) + 1;
} else {
let productToStore = JSON.stringify([product]);
localStorage.setItem("productsInCart", productToStore);
numberOfProducts.style.display = "block";
numberOfProducts.innerHTML = 1;
};
And then in the console:
enter image description here
That circled 2 is the console.log() of the JSON.stringify. I have been searching for detailed info about this but nothing seems to work.
The trouble is this line:
let newArray = productsInLocalStorage.push(product);
From the MDN Docs: The push() method adds one or more elements to the end of an array and returns the new length of the array. (emphasis mine)
You're assigning the length of the array to newArray.
This question already has answers here:
Appending the count to duplicates in a javascript string array
(3 answers)
Closed 3 years ago.
Input:-
Here i'm having array object in which natraj is repeating so i need to append some count like given example.
var aa= [
{name:'natraj'},
{name:'ajai'},
{name:'natraj'},
{name:'ajai'},
{name:'barath'},
{name:'ajai'},
{name:'barath'},
{name:'natraj'},
]
output:-
[
{name:'natraj'},
{name:'ajai'},
{name:'natraj_1'},
{name:'ajai_1'},
{name:'barath'},
{name:'ajai_2'},
{name:'barath_1'},
{name:'natraj_2'},
]
Keep a running count:
var aa = [{name:'natraj'},{name:'ajai'},{name:'natraj'},{name:'ajai'},{name:'barath'},{name:'ajai'},{name:'barath'},{name:'natraj'},]
let counts = {}
aa.forEach(o => {
if (counts[o.name]) {
o.name += `_${counts[o.name]++}`
} else {
counts[o.name] = 1
}
});
console.log(aa)
This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 6 years ago.
I'm using knockout js to create a list with a search function.
Result of the console.log:
As you can see the array supposedly has 5 objects in it but then the proto has 0. I don't understand what might be wrong.
var iniList = [
new Spot("Park"),
new Spot("High School"),
new Spot("Soccer Stadium"),
new Spot("Railway Station"),
new Spot("Hospital")
];
var viewModel = {
spots: ko.observableArray(iniList),
filter: ko.observable(''),
search: function(value) {
console.log(iniList);
viewModel.spots.removeAll();
for (x = 0; x < iniList.length; x++) {
console.log("iniList[x]");
if (iniList[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
viewModel.spots.push(iniList[x]);
}
}
}
};
You're running "viewModel.spots.removeAll();" This removes all elements ofthe observable array.
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 8 years ago.
I have an object as follows -
var users = {
room: [1,2,3,4]
}
How do I find if the 3 exists in the room array ?
JS bin
Use indexOf:
var indexOfThree = users.room.indexOf(3);
if(indexOfThree != -1)
{
var three = users.room[indexOfThree];
}
else
{
console.log("not found");
}
it will return -1 if the element isn't found or else it's index in the array.
This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 9 years ago.
I have an Object. How to get number of nested objects in a?
a = {
firstNested: {
name: 'George'
}
secondNested: {
name: 'James'
}
}
I supposed to use .length which is usually used for arrays. What should I do in this case?
Yes, duplicate of above.. Just use:
var a = {1:2, 3:4},
count = 0,
item;
for(item in a) {
count++;
}
alert(count);