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);
}
Related
This question already has answers here:
Using Objects in For Of Loops
(16 answers)
Closed 12 months ago.
I would like to do something for each element in my_var:
// get selected rows of DataTable
let table_data = dtMember.rows({ selected: true }).data();
const COL_INDEX = 4 ; // column index of the column to count
let my_var = table_data.reduce(function (col_to_count, currentValue) {
if (currentValue[COL_INDEX] in kulturen) {
col_to_count[currentValue[COL_INDEX]]++;
}
else {
col_to_count[currentValue[COL_INDEX]] = 1;
}
return col_to_count;
}, {});
I tried:
for (const element of my_var) {
console.log(element);
}
But getting the
TypeError: my_var is not eterable
I tried also my_var.entries() and my_var.keys() which are suggested on the help page of firefox but let to is not a function
How can I do it correctly?
Use in for Objects. of is for iterating over Arrays.
for (const key in my_var) {
console.log(key, my_var[key])
}
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:
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:
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);