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?
Related
I'm making a webapp with the help of JQuery to keep track of goals & habits.
One can add a main goal such as 'Discipline', and then afterwards can attach subgoals or habits to said main goal (e.g. 'work out everyday').
Organizing the array for main goals is obvious;
goals = ['Acceptance', 'Discipline', 'Accountability'];
I however have found no way in JQuery/Javascript to attach/add an array of items to a specific item in ANOTHER array.
Is there an easier way to do this, with JSON for example ?
Thanks in advance for any help offered
You do this by storing an array of objects at the top level, with a property for the child array
var goals = [{
name: "Acceptance",
children:[]
},{
name: "Discipline",
children:[]
},{
name: "Accountability",
children:[]
}];
When it comes to adding your child you just push it to the child array
goals[0].children.push("Work out every day");
Another option is store key/values at the top level
var goals = {"Acceptance":[],"Discipline":[],"Accountability":[]};
Slightly less versatile, but adding an item to a specific element slightly easier
goals["Acceptance"].push("Work out every day");
I have 2 JSON object arrays in my java script and i am trying to figure out a way to output the differences (if any) between the 2.
for example, here is one of the arrays :
[{
"id":1,
"colour":"BLACK",
"size":"SML",
"qty":1,
"sml":"1"
}]
and here is another :
[{
"id":1,
"colour":"BLACK",
"size":"SML",
"qty":1,
"sml":"5",
"lrg":"1"
},
{
"id":2,
"colour":"BLACK",
"size":"SML",
"qty":1,
"sml":"1"
}]
In the above example i would print to the user that one product has been added, the id 1 product has had sml changed to 5 and lrg added. Is there a way to compare the 2 arrays and get the values that are changed / added?
There is a library for getting the structural differences between two objects
https://github.com/flitbit/diff
I am writing a REST api which I want to make idempotent. I am kind of struggling right now with nested arrays and idempotency. I want to update an item in product_notes array in one atomic operation. Is that possible in MongoDB? Or do I have to store arrays as objects instead (see my example at the end of this post)? Is it for example possible to mimic the upsert behaviour but for arrays?
{
username: "test01",
product_notes: [
{ product_id: ObjectID("123"), note: "My comment!" },
{ product_id: ObjectID("124"), note: "My other comment" } ]
}
If I want to update the note for an existing product_node I just use the update command and $set but what if the product_id isn't in the array yet. Then I would like to do an upsert but that (as far as I know) isn't part of the embedded document/array operators.
One way to solve this, and make it idempotent, would be to just add a new collection product_notes to relate between product_id and username.
This feels like violating the purpose of document-based databases.
Another solution:
{
username: "test01",
product_notes: {
"123": { product_id: ObjectID("123"), note: "My comment!" },
"124": { product_id: ObjectID("124"), note: "My other comment" } }
}
Anyone a bit more experienced than me who have anything to share regarding this?
My understanding of your requirement is that you would like to store unique product ids (array) for an user.
You could create an composite unique index on "username" and "username.product_id". So that when the same product id is inserted in the array, you would an exception which you could catch and handle in the code as you wanted the service to be Idempotent.
In terms of adding the new element to an array (i.e. product_notes), I have used Spring data in which you need to get the document by primary key (i.e. top level attribute - example "_id") and then add a new element to an array and update the document.
In terms of updating an attribute in existing array element:-
Again, get the document by primary key (i.e. top level attribute -
example "_id")
Find the correct product id occurrence by iterating the array data
Replace the "[]" with array occurrence
product_notes.[].note
I'm looking for a way to take a bunch of JSON objects and store them in a data structure that allows both fast lookup and also fast manipulation which might change the position in the structure for a particular object.
An example object:
{
name: 'Bill',
dob: '2014-05-17T15:31:00Z'
}
Given a sort by name ascending and dob descending, how would you go about storing the objects so that if I have a new object to insert, I know very quickly where in the data structure to place it so that the object's position is sorted against the other objects?
In terms of lookup, I need to be able to say, "Give me the object at index 12" and it pulls it quickly.
I can modify the objects to include data that would be helpful such as storing current index position etc in a property e.g. {_indexData: {someNumber: 23, someNeighbour: Object}} although I would prefer not to.
I have looked at b-trees and think this is likely to be the answer but was unsure how to implement using multiple sort arguments (name: ascending, dob: descending) unless I implemented two trees?
Does anyone have a good way to solve this?
First thing you need to do is store all the objects in an array. That'll be your best bet in terms of lookup considering you want "Give me the object at index 12", you can easily access that object like data[11]
Now coming towards storing and sorting them, consider you have the following array of those objects:
var data = [{
name: 'Bill',
dob: '2014-05-17T15:31:00Z'
},
{
name: 'John',
dob: '2013-06-17T15:31:00Z'
},
{
name: 'Alex',
dob: '2010-06-17T15:31:00Z'
}];
The following simple function (taken from here) will help you in sorting them based on their properties:
function sortResults(prop, asc) {
data = data.sort(function(a, b) {
if (asc) return (a[prop] > b[prop]);
else return (b[prop] > a[prop]);
});
}
First parameter is the property name on which you want to sort e.g. 'name' and second one is a boolean of ascending sort, if false, it will sort descendingly.
Next step, you need to call this function and give the desired values:
sortResults('name', true);
and Wola! Your array is now sorted ascendingly w.r.t names. Now you can access the objects like data[11], just like you wished to access them and they are sorted as well.
You can play around with the example HERE. If i missed anything or couldn't understand your problem properly, feel free to explain and i'll tweak my solution.
EDIT: Going through your question again, i think i missed that dynamically adding objects bit. With my solution, you'll have to call the sortResults function everytime you add an object which might get expensive.
I currently have this JavaScript array. I have used this sorted list of numbers to generate a list of tournament brackets where a team's seed is taken into account.
The problem is that to carry out my seeding algorithm, I work out the 2 teams in the final, and use their seed to work out the previous matches, which would be using seeds[0] and seeds[1] giving me 1 and 2. But this causes problems when I'm trying to manually enter the name of the team.
Here is the current array:
var seeds = [
1,2,3,4,5,6,7,8
]
I would like to use something where I can refer to seeds[0].id to get their seed number, and when it comes to printing out their name for the tournament brackets I can use seeds[0].name such that:
var seeds = [
{ id : 1, name : "Team 1" },
{ id : 2, name : "Team 2" },
etc
]
What do you think the best way to do this would be? The main problem is that when creating a list of matches to be placed into the brackets which are seeded, I am using the seed number, and then creating a new array of the seeds in the correct seeded order so that I can just print the brackets as newSeeds[0] vs newSeeds[1] & newSeeds[2] vs newSeeds[3] and so on.
Any help appreciated.
I would suggest this way,
Create a class seed which has all the properties of seeds as instance variables.
When creating the seeds array, keep pushing those seed objects into the array instead of any random hashmap.
So when you iterate over the array, you can call methods on each object(seed object) which can do the desired thing for you.