I have one array of objects containing the menu for my multi select dropdown
const menu = [
{
label: "test 1",
value: "test1",
},
{
label: "test 2",
value: "test2",
},
// so on
]
I have another array of object that is storing selected multiple values from the menu:
const values = [
{
label: "test 1",
value: "test1",
}
// It can contain more than one values since the dropdown is multiselect
]
Remember that menu array contains all the options that I am displaying in my dropdown. Now, what I want is to filter out the selected values from the dropdown. For this purpose, I will need to filter out info from menu array of objects and for that filtration, I will be using values array of objects so that the objects inside the two arrays can be compared and filter out from my menu array of objects.
I have tried numerous JS methods but nothing is working in my case. I cannot use includes method because it compares values by reference and not by value. This is the reason why my condition fails since JS considers both of the objects to be different (their address is different), although they have the same value.
Looking forward to a solution.
selectedValues = values.map(item => item.value)
menu.filter(item => selectedValues.includes(item.value))
This code is sorting by different fields within the document and some of those fields are arrays of objects with the key I want to sort by. I don't understand the behavior that I am seeing when I run the following queries. I don't have a lot of experience with mongodb and didn't write these queries.
const dbCursor = connection.collection('container').find({});
cursor.sort({ "titles.title": 1 });
// These don't happen one after the other as shown here. It's either or.
cursor.sort({ "dates.start": 1 });
Titles and dates are both arrays of objects containing the key passed. Dates appears to be sorting by start date even though there may be multiple object with the the key start in it. Title is not sorting alphabetically and appears to be very random. I don't understand what is actually happening when this type of sort is performed in MongoDB.
How is Mongodb handling the array?
Is it only checking the first element in the array?
Is it checking all the elements in the array?
Is there a better way to perform this type of sorting when dealing with arrays?
// one
{ "titles": [{ "title": "Zippy Mississippi Race" }, { "title": "Wacky
Races"}] }
// two
{ "titles": [{ "title": "New Looney Tunes" }, { "title": "Your Bunny
or Your Life/Misjudgment Day" }] }
// three
{ "titles": [{ "title": "Why Oh Why Wyoming" }, { "title": "Wacky Races" }] }
Returns in this order
Update:
So I have discovered that its sorts all the elements in the array. I just don't understand how it determines who the winner is. Can anyone explain why this order is correct based on sorting all the elements in the array?
I am not sure if there is a set data structure in JS like in python. I have an array of objects like this in Javascript, the ID is unique. If I have two arrays like this - how can I combine them into a single array where I throw away duplicates based on the ID field?
First array:
[{
filed : "1-Jan-1970",
name: "John Smith",
ID: 1234
}
... (many more items)
]
Second array:
[{
filed : "1-Jan-1980",
name: "John Smith",
ID: 1234
}
... (many more items)
]
In the combined array I only want to keep one item with ID = 1234. I dont care which one is thrown away, how do I do this in Javascript that is also fast? I am looking at combining two lists with a couple of thousand items each and I want to keep only one record per ID from either array. Is there a compact way to combine the two arrays into one and then weed out the duplicates?
I have a JSON Array like:
[{
"name": "abc", "month":"Jan-15","value":xyz
},{
"name": "bcd", "month":"Jan-15","value":xyz
},{
"name": "abc", "month":"Feb-15","value":xyz
},{
"name": "bcd", "month":"Feb-15","value":xyz
}]
No. of "names" may vary from month to month. But no. of "month" stays the same.
I need to create tabular overview:
Name Jan-15 Feb- 15 ...... Dec-15
abc value value ..... value
bcd value value ..... value
I'm new to Javascript and I really don't know how to get values in right columns and rows. Though I know how to dynamically add rows.
Shall I opted for "pure" solution or is better to check for a framework like e.g. AngularJS ?
Edit: I have extracted "Months" and "Names" from array and I need to do something like: value = check for this particular "name" in array and return corresponding "value".
How do I write this in JS?
It depends on the form of your data object. If you have an array object or a string object you need to use JSON.parse in order to be able to use indices and values.
If yes, then you will need to have JSON.parse(data).name to return your name values.
JSON.parse(data).month for your months and of course JSON.parse(data).value to return the xyz values.
If your data is already a JSON object then you will simply use data.name, data.month and data.value accordingly.
These links might help you get into the basics, follow the syntax and the examples.
http://www.w3schools.com/js/js_json.asp
http://www.w3schools.com/json/json_eval.asp
http://www.json.org/js.html and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I am trying to optimize accessing cost of nested objects. I have the following structure (example):
Now I want to access data but the problem is I need to keep on adding loops where every I got nested data. That means if I want to access racks I need to itterate 3 for loops like
var jsonObj=[{
"shelfs": [
{
"Shelf1": [
{
"Racks": [
{
"Rack1": [
{
"Book1": "Value"
}
]
},
{
"Rack2": [
{
"Book1": "Value"
}
]
}
]
}
]
},
{
"Shelf2": [
{
"Racks": [
{
"Rack1": [
{
"Book1": "Value"
}
]
},
{
"Rack2": [
{
"Book1": "Value"
}
]
}
]
}
]
}
]
}];
for(var i=0;i<jsonObj.length;i++)
{
var shelfs=jsonObj[i];
var key=Object.keys(shelfs)[0];
//var shelfs=arr[arr[0].key];
//alert(JSON.stringify(shelfs[key]));//shelfs));
for(var j=0;j<shelfs[key].length;j++)
{
var shelfdetails=shelfs[key][j];
var skeys=Object.keys(shelfdetails);
for(var k=0;k<skeys.length;k++)
{
var racks=shelfdetails[skeys[k]];
alert(JSON.stringify(racks));
}
}
}
Here to access racks information I put 3 nested for loops but eventually it is increasing the time complexity. Please can anybody suggest me better data structure or method to access nested JavaScript objects with low time complexity?
You have n books that you want to display in your UI. It will take n display operations to display n books. It does not matter that they are in nested loops, the total number of display operations is still n. There is no optimization you can perform to reduce the number of display operations you need to perform.
Even if you were to flatten your data structure in to a single flat array of books the number of display operations would still be n.
I am trying to optimize accessing cost of nested objects.
Do you mean the CPU cost, the storage cost, or the code complexity cost? The three have quite different implications. Since you go on to say
I need to keep on adding loops whereever I got nested data.
I am going to assume that you are most interested in code complexity. In that case, consider the following flatter data structure, which might be easier to loop through, to filter, to sort, to group, and to otherwise process using utility libraries such as underscore.
[
{ shelf: 'Shelf1', rack: 'Rack1', book: 'Book1', value: "Value"},
{ shelf: 'Shelf1', rack: 'Rack2', book: 'Book1', value: "Value"},
{ shelf: 'Shelf2', rack: 'Rack1', book: 'Book1', value: "Value"},
{ shelf: 'Shelf2', rack: 'Rack2', book: 'Book1', value: "Value"}
]
Abstractly speaking, each "Book1": "Value" item has associated with it a shelf and a rack. In your suggested data structure, this "association" is represented by "belonging to" relationships, where it belongs to an array which is the value of a property whose name specifies the shelf or rack. In the above flatter structure, the associations are instead specified explicitly by giving them as properties.
With the flatter structure, if for some reason you wanted to create a data object with keys giving the shelf and values giving an array of objects on that shelf, in Underscore that is as easy as
_.groupBy(obj, 'shelf')
So all else being equal it seems that the flatter data structure is a more flexible way to represent the data, and you can derive other things you need from it more easily.
Another way to look at it is that currently in order to find the sets of relationships of shelves, racks, and books, you need to iterate through three levels of nested arrays, whereas in the flatter structure the relationships are represented more directly.
Performance, either CPU-wise or space-wise, is rarely going to be a reason to choose one structure over another, unless you are dealing with a huge amount of data. Otherwise, the difference in performance is likely to measured in milliseconds or microseconds, or a few K of storage. You should choose the structure that allows you to represent your algorithms in a fashion which is concise and provably correct. If you intend to handle hundreds of thousands of objects, then in that case yes, you would want to design custom structures optimized for time or space.