Currently code is as given below,
var child_tids = [];
$('.term-selection').each(function(index){
var field = $(this).attr('field');
child_tids.push($(this).val());
});
I need to segregate it based on the attribute 'field'
So I tried the below
var child_tids = [];
$('.term-selection').each(function(index){
var field = $(this).attr('field');
child_tids[field].push($(this).val());
});
But it gives me the error in console "Cannot Read property push of undefined, how can I implement it?
child_tids would need to be an object, and then create an array for each field you can try:
var child_tids = {};
$('.term-selection').each(function(index){
var field = $(this).attr('field');
if (!child_tids[field]) child_tids[field] = [];
child_tids[field].push($(this).val());
});
What you need is an object and not an array. Use something like:
var child_tids = {};
$('.term-selection').each(function(index) {
var field = $(this).attr('field');
if (typeof child_tids[field] != "object") child_tids[field] = [];
child_tids[field].push($(this).val());
});
Related
why do some people store a var within another var?
For example:
var checked = $("#filterControls :checkbox:checked");
var arr = checked
A more expansive view of the code below.
var checked = $("#filterControls :checkbox:checked");
if (checked.length) {
rows.hide(200);
var arr = checked
.map(function() {
return "." + $(this).val();
})
.get();
var selector = arr.join("");
$(selector).show(200);
}
You got a wrong idea about the map function. The map function is immutable, but it returns the modified entries. Because of that you have to assign it to a new value. So checked has the unmodifed values and arr has the values which were modified by map-function.
I'm trying to push the first and last initial of a user to an object array of my users contact information using angular.forEach and I'm not doing it right.
Here is the code I have:
var firstInit = '';
var lastInit = '';
angular.forEach(self.contacts, function(value, key){
firstInit = key.first_name.charAt(0);
lastInit = key.last_name.charAt(0);
this.push(firstInit + ':' + lastInit);
});
But I'm getting TypeError: Cannot read property 'charAt' of undefined
Any advice?
Update:
I changed the code to the following and now I can get the values, but when I try to push onto the contacts object, I'm getting "push" is not a function. Though if I take away the "key" I can push the initials, but they are on the root of the array and not where I want them to be in each object.
Here is the code:
var firstInit = '';
var lastInit = '';
angular.forEach(self.contacts, function(value, key){
firstInit = self.contacts[key].first_name.charAt(0);
lastInit = self.contacts[key].last_name.charAt(0);
self.contacts[key].push(firstInit + lastInit);
});
Either key.first_name or key.last_name is undefined in your contacts somewhere. This is a possible solution:
var firstInit = '';
var lastInit = '';
angular.forEach(self.contacts, function(value, key){
if (key.first_name)
firstInit = key.first_name.charAt(0);
if (key.last_name)
lastInit = key.last_name.charAt(0);
this.push(firstInit + ':' + lastInit);
});
Here's the fix:
var inits = '';
var newContacts = [];
angular.forEach(self.contacts, function(value, key){
console.log(value.first_name);
inits = self.contacts[key].first_name.charAt(0) + self.contacts[key].last_name.charAt(0);
value.userInitials = inits;
newContacts.push(value);
});
Alternatively, I could have just used "value" instead of getting the index/key. That got me the name fields. I created a new object array and pushed the value. I added the new key/value pair with dot notation.
I mostly develop in Java, but I presently need to do some work using JavaScript.
I have data that I need to group by two dimensions: first by gender, and then by language. The object to be grouped comes like so:
var items =[ {
"language":"english",
"gender":"male",
...//twelve other fields
},
...
]
This is what I've tried:
var myMap = {};
for(var i=0; i<items.length;i++){
var item = items[i];
var _gender = item["gender"];
var _lang = item["language"];
myMap[_gender][_lang].push[item];
}
I assumed the above would work, but it’s not working. It keeps returning this error:
Uncaught TypeError: Cannot read property 'undefined' of undefined
Note: In Java, I would be creating a map of arrays.
One problem:
When you call myMap[_gender][_lang].push[item];, what you are actually doing is adding a key to the Object myMap, with the current value of _gender, and turning it into an Object, which you then create a new key for, set to the value of _lang. In addition, .push() is a function used with arrays, to add a new item onto the end of the array. If you want to add a new key and value to an Object, all you have to do is just call that key and assign it a value. Here's an example.
var obj = {
ex1 : 'example 1',
ex2 : 'example 2'
};
console.log(obj);
obj.ex3 = 'example 3';
console.log(obj);
//The code below will simply log 'undefined' in most consoles(it doesn't happen to do so in this snippet).
console.log(obj.ex4);
Here's my suggestion(assuming I correctly understand what you're trying to do):
var items = [{
"language":"english",
"gender":"male",
},
//...more below
];
myMap = [];
for(var i = 0; i < items.length; i++){
myArray = [];
var item = items[i];
var _gender = item["gender"];
var _lang = item["language"];
myArray.push(_gender);
myArray.push(_lang);
myMap.push(myArray);
}
You are doing totally wrong. First of all myMap is object not an array so you can not use myMap.push().
You can achieve like this.
var items = {
"language":"english",
"gender":"male"
}
var myMap = {};
for(var key in items){
var myArray = [];
var _gender = items["gender"];
var _lang = items["language"];
myArray.push(_gender);
myArray.push(_lang);
myMap.item = myArray;
console.log(myMap);
}
for add 2-d array read this. 2-D Array in javascript
when i type the following code statement i get the image attached.Now i want to access the property "label" that is inside each of these objects. how can i go through each of these objects and get their "label" properties?
item.getModel().oData;
i have tried the following code but it failed to work:
var labelText = item.getModel().oData;
labelText.forEach(function(entry) {
var c = entry.StreetName;
});
An example of one of the items:
Because it's an Object and not an Array, you should use Object.keys or some other method to access each property of the object instead. For example:
var labelText = item.getModel().oData;
var keys = Object.keys(labelText);
for (var i = 0; i < keys.length; i++) {
var c = labelText[keys[i]].StreetName;
}
A succinct way of iterating properties of an object, usisng forEach and the keys method of Object
var labelText = item.getModel().oData;
Object.keys(labelText).forEach(function(entryName) {
var c = labelText[entryName].StreetName;
});
var labelText = item.getModel().oData;
$(labelText).each(function(index,entry) {
var c = entry.StreetName;
});
I have this js:
var json = {"products":[{"id":"6066157707315577","reference_prefix":"BB","name":"BeanieBaby","product_line":false,"has_ideas":true},{"id":"6066197229601550","reference_prefix":"BBAGS","name":"BlackBags","product_line":false,"has_ideas":false}],"pagination":{"total_records":4,"total_pages":1,"current_page":1}}
var stuff = json.products.filter(function(obj) {
return obj.has_ideas === true
});
console.log(stuff);
I need help returning a js obj in the same format it started in (var json = {"products":[...) but containing only the filtered objects if they exist. If not, i just want an empty array. How do I do this?
Just create your object and fill in its 'products' property :
var jsonObj = {"products":[{"id":"6066157707315577","reference_prefix":"BB","name":"BeanieBaby","product_line":false,"has_ideas":true},{"id":"6066197229601550","reference_prefix":"BBAGS","name":"BlackBags","product_line":false,"has_ideas":false}],"pagination":{"total_records":4,"total_pages":1,"current_page":1}}
var stuff = {};
stuff.products = jsonObj.products.filter(function(obj) {
return obj.has_ideas === true
});
console.log(stuff);