I have the following array with data:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4"},
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4"},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4"}
]
All in my angular Cart scope:
var data = $scope.cart;
now i would like to add to each row the order number, so i use a push:
$scope.cart.push({"orderNumber": $scope.OrderNumber});
the problem is that is adds the order number but as a separate row and not inside each of the rows. so my desired output should look like this:
[
{"count":1,"title":"Ark: Survival","platform":"Playstation PS4","orderNumber":1 },
{"count":2,"title":"Lara Croft:", "platform":"Playstation PS4","orderNumber":1},
{"count":1,"title":"Madden NFL", "platform":"Playstation PS4","orderNumber":1}
]
how would i push to each row of the array? i tried several versions but can't seem to get it to work.
in order to achieve that you should iterate through each object and set order number separately
angular.forEach($scope.cart, function (value, key) {
value.orderNumber = $scope.OrderNumber;
});
You need to iterate over each item and add the orderNumber as a property for each item. So you can use forEach function of the angular for that.
angular.forEach($scope.cart, (item) => item.orderNumber = $scope.OrderNumber)
just got an answer that i implemented but the user has deleted the response, however it works perfect, this was the solution that i used:
$scope.cart.forEach(item => item.orderNumber = $scope.OrderNumber)
Related
As stated in previous questions here and on Google, I've added a step function to alter the data and provide me with the specific columns I need.
Here was a comment that said to use Lodash _.pick() method: https://stackoverflow.com/a/59944480/4236332
However, when doing that I end up with a completely empty results output.
Code:
parseFile(){
Papa.parse( this.file, {
header: true,
skipEmptyLines: true,
step: (results, parser) => {
results.data = _.pick(results.data , [ 'column1', 'column2']);
return results;
},
complete: function( results ){
console.log(results.data)
this.content = results;
this.parsed = true;
}.bind(this)
});
}
Before vs. After:
First console log holds the colums in the JSON I need plus several I want to filter out, Second log is completely empty.
Tried removing it from the step function and doing it in the complete function but same output.
EDIT 1:
I have tried testing the _.pick function on results.data[0] and this does work, so something is preventing _.pick() of looping through all json records in the list results.data?
Managed to fix this issue by looping through it with a _.map() function. This way it goes through every object in the array with the ._pick() method.
results.data = _.map(results.data, obj => _.pick(obj, [ 'column1','column2']));
I have a list of html elements with data attributes, which I would like to assemble into a jQuery object and manipulate the values.
What is the best way to dynamically add these in an each loop so that I can easily access the data as so: data.name and data.name.prop?
I want all the naming conventions to be dynamic and based on the data.
I based my code on the top answer from here: How to create dynamically named JavaScript object properties?
So far I have:
$('.licences-list .data div').each(function(index) {
var data = {}
cats[$(this).find('p').data('cat')] = $(this).find('p').data('catname')
cats.push(data)
})
But when I try to iterate over the data array, like so:
$.each(cats, function(key, value){
$('<div class="card"><p>'+value+'</p></div>').appendTo('#commercial-licenses');
});
I just get [object Object] output... and I'm not sure why!
var data = {}
cats[$(this).find('p').data('cat')] = $(this).find('p').data('catname')
Each time you loop through, you're actually just adding an empty object (data) to your array (cats). You're then assigning a named property to that array (cats) which $.each has no idea about (it ignores them because it's iterating over an actual array).
My guess is you want an object map which is something like: var cats = { "f1": "feline 1", "f2": "feline " };
In that case what you want is:
var cats = {};
$('.licences-list .data div').each(function(index) {
cats[$(this).find('p').data('cat')] = $(this).find('p').data('catname')
})
If you want an array that contain more values than just strings (or whatever data you have added to the element), you create new objects each time and append them to the cats array:
var cats = [];
$('.licences-list .data div').each(function(index) {
cats.push({
'id': $(this).find('p').data('cat'),
'name': $(this).find('p').data('catname')
});
})
This will then give you an array that you can use $.each over, and access the values using: value.id, value.name
Don't over complicate it.
$('.div').attr('data-attribute', 'data-value');
using your example:
$('.licences-list .data div').attr('attribute-name', 'attribute-value');
I've got objects 'ing' with a field named 'id' and another one called 'fObj' with a field named 'contain'.
By using ng-repeat i'd like to show only these 'ing' objects where ing.id is a part of fObj.contain
e.g.
ing=[{id: 1,field: value},{id:2, field: othervalue},{id:3, field: cat}];
fObj={field1: value1, field: value2, contain: ':1:3:'};
By having this contain value I'd like to show only ing's with id=1 and id=3
Yeah, I know there are two types of data (number and string) but even if i changed numbers to strings it still didn't work
I just dont't know how to make it works. It's probably some kind of custom filter, but I've tried couples and nothing happend.
I would be glad if you suggest me a solution.
Thanks
In your controller,
var ids = fObj.contain.split(':');
// the array for your ng-repeat
var displayIng = [];
// loop the objects, see if the id exists in the list of id's
// retrieved from the split
for(i = 0; i < ing.length; i++) {
if(ids.indexOf(ing.id.toString()) displayIng.push(ing[i]);
}
I would split the numbers out of fObj.contain; and use them as hashmap object keys for simple filtering of the array
var ing=[{id: 1},{id:2},{id:3}];
var fObj={contain: ':1:3:'};
var IDs = fObj.contain.split(':').reduce(function(a,c){
a[c]=true;
return a;
},{});
// produces {1:true,3:true}
var filtered = ing.filter(function(item){
return IDs[item.id];
});
console.log(filtered)
My requirement is to sort array using Dynamic property names using EmberJS.
What i have done previously for single column sorting is
ents = #get('acceptedEntities') //get the array
#set('sortAscending', !#get('sortAscending'))
sort_data = ents.sortBy(property_name) //property name is sort order
And what i am looking id
ents = #get('acceptedEntities')
#set('sortAscending', !#get('sortAscending'))
sort_data = ents.sortBy([property_name1, property_name2])
I tried with the above solution but no luck and i here about computed sort
and implemented like this
model = #get('acceptedEntities')
sortProperties = [property_name, 'entity_sf_account_name']
sort_data = Ember.computed.sort(model, sortProperties)
But Sorting is not going properly, Please give me suggestions to do this.
i tried this too
sortProperties = ['one:asc', 'two:desc', 'three:asc']
sort_data = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: model,
sortProperties: sortProperties
})
the above code works fine for sorting with multiple parameter but when i want sort order its not working properly
Thanks
There are many ways to do this, but I would do it like this:
sortProperties = ['one:asc', 'two:desc', 'three:asc']
sort_data = Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
content: model,
sortProperties: sortProperties
})
I have a large array in this format:
var cars = [{"brand":"Honda","year":"2002"},{"brand":"Toyota","year":"2000"},{"brand":"Subaru","year":"2009"}];
The array is very large (I made it small for demo purpose) and I want to organize cars of the same brand into an array, so the brand name will be the key for each sub-array, like this:
carsByBrand[ honda[], toyota[], subaru[] ]
I expected it would be straight forward but it's not so. I've tried several variations of the following code, but everytime it returns:
carsByBrand[item.brand] is undefined
var carsByBrand = [];
$.each(cars, function(i,item){
carsByBrand[item.brand].push(item);
});
//console.debug(carsByBrand); //uncomment and look at this if you have Firebug
alert(carsByBrand.length);
I've also tried carsByBrand[item['brand']], why doesn't this work, and how to do it?
jsBin: http://jsbin.com/orafos
carsByBrand[item.brand].push(item);
That will only work if the array for that brand already exists, which it does not unless you initialize it first with an empty array.
Try
brand = carsByBrand[item.brand];
if (brand) {
brand.push(item);
else {
carsByBrand[item.brand] = [item];
}