I have an endpoint that has 5 users in a obj with 5 arrays. every 5 data changes for a few properties. I want to get the dynamic property, and push it to an array for each user. How can i do this? I need each user to have their own dynamic array.
watchMe = [{user: 1 hr: 10}, {user: 2 hr: 20}, {user: 3 hr: 30}];
updateMe=[];
function getLive(){
angular.forEach(watchMe, function(value, key){
updateMe.push(value.hr);
console.log (updateMe)
})
};
$interval(getLive, 5000);
I think using a utility library like lodash could provide you with what you're after.
Namely: whenever the interval hits you can use _.filter to get you the objects containing the dynamically added property - or just _.map() through them and pull the property (if it exists).
Disclaimer: I would have preferred to add this feedback as a comment but I do not have the rep yet
Related
I have an array of Obj1. In a project vue 3+ts , after clicking the button "load more", i call via axios the backend service and retrive an array of the same type. I want to append this rows to the previous array, but it is not working :
this.Array1.push(response.data)
If i add 1 item at the time, the Array1 gets updated:
this.Array1.push(response.data[0])
What am i missing?
Based on your question that this code works:
this.Array1.push(response.data.results[0])
Indicates that response.data.results is the array you want to work with. If you want to push that entire array in, you can simply use the ES6 spread operator:
this.Array1.push(...response.data.results)
You're pushing different values. response.data.results is an array, while response.data is a json data with an array.
So, you might want to just do:
this.Array1 = response.data.results
I am new to React JS & currently trying to iterate a certain data to present the same in react js but not able to do the same. The data which looks something like this
Now, the final output should be look something like this in tabular format
The things which I tried are:-
The error which I am getting below one is for 1 image and for second, the code is not getting parsed.
[![error][5]][5]
So, how to achieve the desired output in react js
It looks like you're trying to use map onto an object, while you should use it on a collection. Maybe try something like this :
Object.values(listData.data).map((meetingRoom) => { // your code here });
This will allow you to use the content inside your data object as an array of objects.
Edit : Sorry, I didn't understand you need to access the key as well as the value. To achieve that you can simply use Object.entries which will return the key (Meeting Room 1, Meeting Room 2 in this instance) in the first variable and the array of items in the second variable.
Here's a quick example:
Object.entries(listData.data).forEach(([key, value]) => {
console.log(key, value);
// You could use value.map() to iterate over each object in your meeting room field array.
});
Note : you can also use a for (... of ...) loop like this instead of a forEach :
for (const [key, value] of Object.entries(listData.data)) {
console.log(key, value);
};
For more information about the Object.entries method, feel free to check the MDN Webdocs page about it here.
In our data layer, we have an array of add-ons for our products
Add-ons: [
{code: 'wheels', selection: 'alloy'),
{code: 'finish', selection: 'pearl'}
],
Unfortunately they are not the same for each product and are not returned in the same order.
We would like to create GA events for each add-on selected, using the code and selection in the array as the event Action and Label. So we need to loop through the array and create a new event for each item, and use the elements of the array in the event.
I created a GTM variable that pulls in the array (that works fine), but now I need to fire the event tracking. I've been working with forEach
<script>
{{addon-list}}.forEach(function(arrayItem) {
dataLayer.push({
'event': 'GAEvent',
'eventCategory': 'AddOn',
'eventAction': {{addon-list}}.code,
'eventLabel': {{addon-list}}.selection,
'nonInteraction': 1
})
});
</script>
While it fires one event, it is undefined for the Action and Label. Any ideas? This seems like a handy thing so I'm sure someone has done / is doing it.
Through a lot of iteration, I managed to arrive at a solution.
First, the GTM variable that pulls in the array needs to be version 1 because of how the data gets handled.
Then the script is:
<script>
{{addon-list}}.forEach(createEvent);
function createEvent (addon) {
dataLayer.push({
'event': 'GAEvent',
'eventCategory': 'AddOn',
'eventAction': addon.code,
'eventLabel': addon.selection,
'nonInteraction': 1
})
};
</script>
By declaring the value of addon and using it in the variables to pull in the elements of the array, it worked.
There has to be something simple I am missing here.
http://jsfiddle.net/v9mdZ/
I am just learning Backbone and Underscore/loDash and am trying to get familiar with chain.
I have the following code, which works as expected:
var ids = _.pluck(collection.where({'is_checked':true}), 'id');
I attempted to refactor this, using chain like so:
var ids = collection.chain().where({'is_checked':true}).pluck('id').value();
Why doesn't the refactored code work? Am I using chain wrong?
Solution (details below)
Don't use where with chain.
The merging of some Underscore methods into collections is a little imperfect. When you say collection.some_mixed_in_underscore_method(), the collection unwraps some of the Backbone stuff behind your back so that the Underscore method is applied to the attributes inside the collection's models; it sort of works like this:
var ary = _(this.models).map(function(m) { return m.attributes });
return _(ary).some_mixed_in_underscore_method();
But collection.chain() doesn't work like that, chain just wraps the collection's models directly so if you do this:
console.log(collection.chain());
you'll see that chain is giving you an object that wraps an array of models. Your models won't have an is_checked property (i.e. there is no model.is_checked), they will have is_checked attributes though (i.e. there will be model.get('is_checked') and model.attributes.is_checked).
Now we can see where everything goes wrong:
collection.chain().where({'is_checked':true})
The models don't have is_checked properties. In particular, there won't be any models where is_checked is true and everything after the where is working with an empty array.
Now that we know where things go sideways, how do we fix it? Well, you could use filter instead of where so that you can easily unpack the models:
collection.chain()
.filter(function(m) { return m.get('is_checked') })
.pluck('id')
.value();
But, your models don't have ids yet as you didn't create them with ids and you haven't talked to a server to get ids so you're going to get an array of undefineds back. If you add some ids:
var collection = new App.OptionCollection([
{id: 1, 'is_checked': true},
{id: 2, 'is_checked': true},
{id: 3, 'is_checked': false}
]);
then you'll get the [1,2] that you're looking for.
Demo: http://jsfiddle.net/ambiguous/kRmaD/
I'm attempting to learn backbone.js and (by extension) underscore.js, and I'm having some difficulty understanding some of the conventions. While writing a simpel search filter, I thought that something like below would work:
var search_string = new RegExp(query, "i");
var results = _.filter(this, function(data){
return search_string.test(data.get("title"));
}));
But, in fact, for this to work I need to change my filter function to the following:
var search_string = new RegExp(query, "i");
var results = _(this.filter(function(data){
return search_string.test(data.get("title"));
}));
Basically, I want to understand why the second example works, while the first doesn't. Based on the documentation (http://documentcloud.github.com/underscore/#filter) I thought that the former would have worked. Or maybe this just reflects some old jQuery habits of mine... Can anyone explain this for me?
I'd guess that you're using a browser with a native Array#filter implementation. Try these in your console and see what happens:
[].filter.call({ a: 'b' }, function(x) { console.log(x) });
[].filter.call([1, 2], function(x) { console.log(x) });
The first one won't do anything, the second will produce 1 and 2 as output (http://jsfiddle.net/ambiguous/tkRQ3/). The problem isn't that data is empty, the problem is that the native Array#filter doesn't know what to do when applied to non-Array object.
All of Underscore's methods (including filter) use the native implementations if available:
Delegates to the native filter method, if it exists.
So the Array-ish Underscore methods generally won't work as _.m(collection, ...) unless you're using a browser that doesn't provide native implementations.
A Backbone collection is a wrapper for an array of models, the models array is in c.models so you'd want to:
_.filter(this.models, function(data) { ... });
Backbone collections have several Underscore methods mixed in:
Backbone proxies to Underscore.js to provide 28 iteration functions on Backbone.Collection.
and one of those is filter. These proxies apply the Underscore method to the collection's model array so c.filter(...) is the same as _.filter(c.models, ...).
This mixing-in is probably what's confusing the "should I use the native method" checks that Underscore is doing:
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
You can use _.filter on a plain old object (_.filter({a:'b'}, ...)) and get sensible results but it fails when you _.filter(backbone_collection, ...) because collections already have Underscore methods.
Here's a simple demo to hopefully clarify things: http://jsfiddle.net/ambiguous/FHd3Y/1/
For the same reason that $('#element') works and $#element doesn't. _ is the global variable for the underscore object just like $ is the global variable for the jQuery object.
_() says look in the _ object. _filter says look for a method named _filter.