admin管理员组文章数量:1403361
I have this javascript object:
{"Vanilla": 36, "Chocolate": 50, "Stracciatella": 24, "Coffee": 18}
to which I would like to "add" this array:
["Vanilla", "Amarena", "Chocolate", "Pistachio"]
The result I want is this:
{
"Vanilla": 37,
"Chocolate": 51,
"Stracciatella": 24,
"Coffee": 18,
"Amarena": 1,
"Pistachio": 1
}
This is what I have tried:
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
}
var keys = Object.keys(all);
for (var i = 0; i < me.length; i++) {
if (me[i] in keys) {
console.log("keys:" + keys + " - " + me[i] + " in keys");
} else {
console.log("keys:" + keys + " - " + me[i] + " not in keys");
};
}
console.log(keys);
I have this javascript object:
{"Vanilla": 36, "Chocolate": 50, "Stracciatella": 24, "Coffee": 18}
to which I would like to "add" this array:
["Vanilla", "Amarena", "Chocolate", "Pistachio"]
The result I want is this:
{
"Vanilla": 37,
"Chocolate": 51,
"Stracciatella": 24,
"Coffee": 18,
"Amarena": 1,
"Pistachio": 1
}
This is what I have tried:
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
}
var keys = Object.keys(all);
for (var i = 0; i < me.length; i++) {
if (me[i] in keys) {
console.log("keys:" + keys + " - " + me[i] + " in keys");
} else {
console.log("keys:" + keys + " - " + me[i] + " not in keys");
};
}
console.log(keys);
Share
Improve this question
edited Nov 15, 2015 at 12:32
Andreas Louv
47.1k13 gold badges107 silver badges126 bronze badges
asked Nov 15, 2015 at 11:55
Ken AraKen Ara
431 silver badge7 bronze badges
1
- 1 Wele to Stack Overflow! Please take the tour, have a look around, and read through the help center, in particular How do I ask a good question? Any code related to your question must be in your question, not just linked. Links rot, making the question and its answers useless to people in the future, and people shouldn't have to follow some random link to help you. If the question doesn't make sense and can't be answered without the link, it's not appropriate for this site. Instead, put the minimum plete example in the question. – T.J. Crowder Commented Nov 15, 2015 at 12:02
4 Answers
Reset to default 4This works
the first part of the code in your fiddle
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
}
Now the answer:
me.forEach(function(key) { // forEach iterates through an array
all[key] = (all[key] || 0) + 1; // create new item if it doesn't exist
});
The forEach() method executes a provided function once per array element.
For a more detailed description of .forEach see MDN documentation where the above description came from
as this question was tagged jQuery
, lets add some jQuery
code, just for laughs
$.each(me, function(unusedIndex, key) {
all[key] = (all[key] || 0) + 1;exist
});
jQuery.each() A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties. - source http://api.jquery./jquery.each/
Here's me playing with myself - http://jsfiddle/7ephp3f4/1/
The following code allows you to increment the values within your object stock
based on the keys in your array stockUpdate
:
var stockUpdate = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];
var stock = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
};
for (var i=0; i < stockUpdate.length; i++) {
var stockType = stockUpdate[i];
if (stock[stockType] === undefined) {
stock[stockType] = 0;
}
stock[stockType]++;
}
you are almost there,
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"]
var all = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
}
var keys = Object.keys(all);
for (var i = 0; i < me.length; i++) {
if (keys.indexOf(me[i]) >= 0) {
console.log("keys:" + keys + " - " + me[i] + " in keys \n");
all[me[i]]++;
} else {
console.log("keys:" + keys + " - " + me[i] + " not in keys \n");
all[me[i]] = 1;
};
}
console.log(all);
you can also use reduce to "merge" the newItems
into items
var items = {"Vanilla": 36, "Chocolate": 50, "Stracciatella": 24, "Coffee": 18}
var newItems = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];
var result = newItems.reduce(function(pre, curr) {
// use double == to check when the item is either undefined or null
pre[curr] = (pre[curr] == null? 0 : pre[curr]) + 1;
return pre;
// pass items as the initial data to reduce
}, items);
console.log(result);
The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
in this case, I reduce the newItems
list, from left to right, calcuate the count in the items
object, and return the calculated items.
It is a good moment to use http://underscorejs:
var me = ["Vanilla", "Amarena", "Chocolate", "Pistachio"];
var all = {
"Vanilla": 36,
"Chocolate": 50,
"Stracciatella": 24,
"Coffee": 18
};
_.each(me, function(name) {
if (_.has(all, name)) {
all[name]++;
} else {
all[name] = 1;
}
});
本文标签: jqueryIncrement Javascript object values by quotaddingquot an arrayStack Overflow
版权声明:本文标题:jquery - Increment Javascript object values by "adding" an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744359365a2602468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论