How can we just take the value of group names with vuejs? - javascript

I have an array checking for a REST API and which includes group names and group names include keys and values. I want to take just values of keys. Because keys in group names and values in keys. I just wanna see the values on the network side in properties.
I tried to use lodash components like a grubby,reduce,and omit etc. but ı couldnt make it work.

Related

underscore .find() multiples with same key value

I am currently trying to filter an object via POST from a form the form is made of checkboxes and as a result I need to be able to search an array of objects for multiple values on the same key.
My POST looks similar to this,
{
tax_year: ['2016/17', '2017/18'],
status : ['completed'],
user : [1,4,78]
}
How would I go about search an array of objects and returning all the objects that have a matching key and value? I know I can do a single find with underscore like so,
var result = _.where(historyData, {tax_year: "2016/17"});
but I have no clue as to how to search for multiple matching keys and values?
It seems that you're matching on both of the values in an array value (regardless of whether there are more array values or not). If that's true, you might have to use the _.filter() method, where you can provide whatever logic you need in the 'predicate'. Alternatively, you could add a unique identifier on the server side to each record and match on that.

how to split grouped array of objects(with same id) into individual array of objects using javascript

May i know how to split the arrays which are grouped.
Attached is the snapshot showing my requirement.
The grouped array objects have similar parent Id, i would want to separate them out and place them as individual array objects like the rest of the array objects.
Thanks kg

Use array or JSON to keep track of unique list

I have an object in Parse .com database that can receive thumbs up votes. I want to keep track of which users have provided a thumbs up to prevent the same user doing it again. I am thinking of adding a field to my object called "voters" that will hold the list of voting users.
Should this be an array of usernames or a JSON with keys as the usernames?
Why would I use an array if i could use a json map and do O(1) lookup of the names on the keys?
ie object.voters[uername] = undefined then no votes?
Use Array if you need just the name of the voter .
Use Json if you need more info than just the name .
Why is that if u need just the name just making an array with the voter names first will be easy second it will be faster t process .
but if you need something like
voter={"name":"foo","age":"25"}
as you see we can use the powerof json to save multiple info about one user and anyway each voter must be within a single array so you can iterate over them
UPDATE---
to find the index of a name in an array just use array.indexOf("the name") what this will return is the index number of the name if the name doesnt exist it will return just -1 so a simple if else can handle this
UPDATE---
And here i found a performance test to compare which is faster to lookup whitin an array or an object and seems arrays still win this.
And here is another source for reading

How to use typed arrays to simulate objects?

Objects in JavaScript contain key-value pairs. The cost of a typical pair (using DevTools Profiler) is a reference to the key name, 8 bytes, and the cost of the object: 4 bytes for small ints, 8 bytes for numbers, references, etc.
The cost of the keys add up, especially in arrays with millions of objects.
Is there a asm.js sort of way to use typed arrays for arrays of identical objects?
Yes, I know this seems like a pain, but for a particular project, this may be required.
The sort of approach I'm thinking of is using a template JS object who's keys describes the offset into a typed array for each key's value, along with its type. For arrays of these objects, there'd be multiple of these object spans.
Thus two questions:
1 - Are my assumptions correct .. and there is no optimization in chrome/modern browsers that optimize the key costs? Possibly with constraints used here: http://www.2ality.com/2013/08/protecting-objects.html
2 - If so, is there a library for handling typed arrays as objects? Or any articles or gists etc?
If you have millions of objects, all with the same set of known keys and you have so many of them that memory is an issue, then you probably don't want to store your data as javascript objects at all.
You probably want to think about this like a database problem. You want:
A semi-compact storage format
A way to find the right record in the storage format (this depends upon what the data is and how you need to access it.
A way to read the semi-compact storage format and turn it into a live javascript object only when you want to actually use that object in your code.
A way to write changes back to the semi-compact storage format.
For example, if you had seven keys (e.g. fields) and three were numbers and four were strings and one of the numbers was your lookup key, then you could do this:
Create three typed arrays (one for each numeric value)
Create one regular array. This will hold all the string values concatenated together with a unique separator character.
Create a master key lookup object
As you read your data in, presumably from multiple ajax calls, you do the following:
Note the length of one of your arrays (they are all the same length) as this will be your new record number.
Add the lookup key to the master key object and set the value of the key to be the record number.
Add each numeric value to each typed array (each will be the record number index in the array)
Concat all the string values together with a separator between them in order by key and then put the contactenated string value into the string array.
Now you have a semi-compact storage format and a key lookup means. When you want to lookup a value, you use the master key lookup object. The value for the key will be the record number which is an index into the other arrays. You can create two functions that will find a record and return a javascript object form of the record (all data in key/value pairs on the object) and another function that will write an object (that might have changed = but the master key can't change) back to your storage format.
This makes a few assumptions about your data that you have one master key that won't change that you use for lookup and that you can find a separator to bind all the string values together and then separate them apart later and that you can know when you go to store all this and that you know what the keys are and that the objects generally all have the same keys.
If any of those assumptions are not true, then the design would have to be adapted to deal with that, but hopefully you get the idea of using something other than a giant array of objects to store your data and then constituting a given object only when you need to work with that record's data.

Do undefined values in Javascript arrays use any memory or get iterated over in a for-in loop?

I'm building an Entity System for a game, and basically I'm not sure whether I should use simple objects (dictionaries) or arrays to to store the entities/components by their id.
My biggest issue is that I didn't want a dynamic entity id. If the id was just a string (using the dictionary to store entities), then it would always be valid and I could use storage[id] to get to that entity.
If I used arrays, I thought, the id's of entities, which would represent an index in the storage array, would change. Consider this array of entities:
[
Entity,
Entity, //This one is being removed.
Entity
];
If I was to remove the second entity in that array, I thought that the id required to access the third array would have to change to the id (index) of the (now removed) second entity. That's because I thought about deleting in terms of splice()ing.
But, I could use the delete expression to turn the element (an entity) into an undefined! And, if it's true that arrays in Javascript are actually just objects, and objects logically have infinitely many undefined values, does that mean that undefined values inside arrays don't use up memory?
Initially, I though that arrays were implemented in a way that they were aligned in memory, and that an index was just an offset from the first element, and by this logic I thought that undefined values would use at least the memory of a pointer (because I, actually, thought that pointers to elements are aligned, not elements themselves).
So, if I stored 10k+ entities in this array, and deleteed half of them, would the 5k undefined's use any memory at all?
Also, when I do a for entity in array loop, would these undefined elements be passed?
Also, where can I find resources to see how arrays are actually supposed to be implemented in Javascript? All I can find are general explanations of arrays and tutorials on how to use them, but I want to find out all about these little quirks that can prove important in certain situations. Something like a "Javascript quirks" site would be great.
Arrays are not just objects. In particular the length property is very magic.
Of course, a JavaScript engine is allowed to represent the array internally in any way it chooses, as long as the external API remains the same. For instance, if you set randomly separated values then they may be stored as a hash, but if you set consecutive values then they may be optimised into an array.
for ... in does not enumerate properties that are not set. This includes an array literal that skips values e.g. [true, , false], which will only enumerate indices 0 and 2.

Categories

Resources