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);
Related
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:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
How do I search all values inside all objects in an array object in javascript?
const array = [
{
id: 145,
name: "Test"
},
{
id: 241,
name: "Array"
}
]
if I run a function it returns find('a') it returns the second object or if i run find(1) it returns both of the object
something like this
var matches = [];
for(i=0;i<array.length;i++)
{
if(array[i].name.indexOf('a') > -1)
{
matches.Push(array[i]);
}
}
return matches;
This isn't valid javascript as such, but that is how you would write something like this in javascript.
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
This question already has answers here:
How do I iterate over a JSON structure? [duplicate]
(13 answers)
Iterate over Javascript object [duplicate]
(1 answer)
Closed 8 years ago.
var questions = {
game: {
question_1: {
question: "Question 1",
points: 7
},
question_2: {
question: "Question 2",
points: 5
}
}
};
New to programming...
How do I use a for-loop to iterate over my questions object to access questions.game.question_1.question? Or questions.game.question_[n].question
var answers = [];
for(var i=0; i <questions.game.length; i++) {
questions.game.question_[i].question.push(answers);
}
Like this:
for (var key in questions.game) {
if (questions.game.hasOwnProperty(key)) {
questions.game[key].question.push(answers);
}
}
On the other hand, you're going to run into some trouble when you try to push an array into a string. Did you have that backwards?
for (var key in questions.game) {
if (questions.game.hasOwnProperty(key)) {
answers.push(questions.game[key].question);
}
}
This question already has answers here:
How to remove item from array by value? [duplicate]
(37 answers)
Closed 8 years ago.
My code looks like this. At the beginning I have defined nested arrays
var license= [{
packet: [{
pay: [],
channel: [],
fun: []
}]
}];
Add item work corect
function addPayChannelToObject(name) {
var paychannel = { name: name };
license[0].packet[0].pay.push(paychannel);
console.log(pay);
}
How can I delete an item by value?
function removeItemFromObject(name) {
//??
console.log(pay);
}
Try this way
function removeItemFromObject(name) {
var index = license[0].packet[0].pay.indexOf(name); // found index of value
if(index > -1){
license[0].packet[0].pay.splice(index, 1); //remove index
}
console.log(pay);
}