I have a question on what's the best way to achieve this,
I have an array with 8 objects:
myArray = [ {id: 1, name, xxx, town: Town}, ...]
then I have a second array with another 8 objects of type Town:
townArray = [ {id: 1, name: RngTown}, ... ]
I subscribe to the first array, then I subscribe to the second, and what I want to achieve is a new array that fills the property Town of myArray, with the 8objects in townArray, order doesn't matter, it's just dummy data.
is map() the right way?
and in this example I have 8 objects in the first array, and 8 in the second, so each property Town will have a unique Town from the second array, but what would happen if I only had 6 town object? Can I map all of them to my 8objects, and when they finish after 6 towns, they start from the first town again until all objects in the first array have a town?
thank you
I did an example here.
I think this will solve your issue!
What it does is assign the towns by the id, so the result 1 will have the town 1 and so on!
stackblitz
let me know if you really want to use map.
Check in the console log the result!
Related
I'am new to Angular and TypeScript, i'am trying to implement this one but i couldn't make it possible.
My question is i have an array with multiple nested arrays. every nested array has 'id' value. i need get all the nested array 'id' values and store those values to the new array.
this is what i have right now
[parentArray]
0: nestedArray
count: 0
no: null
level: null
id: 1
1: nestedArray
count: 0
no: null
level: null
id: 22
2: nestedArray
count: 0
no: null
level: null
id: 34
I want an array like this, example - idInformation = [1,22,34].
I tried with filter but it gives all the values, i don't know exactly how to do this in TypeScript. please help me to get this or any help also appreciable.
It is not about typescript nor about angular, you should get familiar with basic javascript.
And looking at your data sample I suppose you have array of objects, not array of arrays.
const ids = array.map(v => v.id);
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'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 have an array of approximately 19.000 items.
I'll have to access them by an arbitrary id at random (that is, there's no need to traverse the array)
I was just wondering if js con optimize the code if I use the id as the index of the array, or if there's any kind of trick or library to speed up these kind of things.
To be more precise, I'll have the results of an election in approximately 20k schools, and I'd like to know your advice about which one would be faster:
[
{
school_id: xx
results: [
{
party_id: xx
votes: xx
}, [...]
]
}, [...]
]
[ // use school_id as index to the array
[
{
party_id: xx
votes: xx
}, [...]
], [...]
]
The question is if js is smart enough to optimize array random access.
And any tool you could advice me to use to test the performance would be much welcome
These questions are always engine-dependent. In V8 (Google Chrome, Node.js):
Objects and Arrays are not radically different. For implementation simplicity, all objects have an external elements array where properties that are positive integers are stored.
So when you do obj[5], it doesn't matter if obj is the Javascript Array object or any javascript object - it will access the object's external elements array.
So if you created an object like this:
var a = {
a: 3,
b: 4,
c: {},
5: 5,
6: 6
};
The object layout will be:
[HiddenClassPointer, PropertiesArrayPointer, ElementsArrayPointer, TaggedSmallInteger(3), TaggedSmallInteger(4), JSObjectPointer]
Note how the named fields are stored side by side with the internal fields. If you now add any property after the fact, it will
go into the external properties array pointed by the second field instead of stored on the object directly.
The "fields" with the integer key would be in the external elements array pointed to by ElementsArrayPointer like this:
[HiddenClassPointer, TaggedSmallInteger(25), TheHolePointer, TheHolePointer, TheHolePointer, TheHolePointer, TheHolePointer, TaggedSmallInteger(5), TaggedSmallInteger(6), ...more hole pointers until 25 elements]
The 25 is length of the backing array. I will come back to that soon.
The hole pointer is needed to disambiguate between explicit undefined values given from the user and actual holes in the array. When you try to retrieve a[3], it will
return you undefined because there was a hole. So the actual hole object is not returned to user. So there are actually 3 different types of null :P
The initial length of 25 comes from the formula (initial index + 1 ) + ((initial_index + 1 ) / 2) + 16 so 6 + 7/2 + 16 = 25. You can see it in a heap snapshot.
( 108 - 8 ) / 4 === 25
Write a test using JSPerf. You can test a number of different scenarios using it.
I have a set of variables which represent prices and number of items sold in a 2d array. I
have sorted it in order to find the lowest price.
I'd like to set the second variable (number sold) of the first item (player A) to a value (200) by referring to the array.
For example:
var playerASold;
var arr=[
[playerAPrice,playerASold],
[playerBPrice,playerBSold],
[playerCPrice,playerCSold]];
arr[0][1]=200;
this doesn't work, probably because playerASold currently has a value of 0 and it is trying to set 0=0.
How do I refer to the variable and not the value of the variable?
JavaScript has no notion of C's pointers or C++'s references, so you'll have to do it in a different way. Rather than trying to store references in an array, try making the array the sole holder of the data.
That might look like this:
var players = [
{ price: 5, sold: 1 },
{ price: 3, sold: 6 },
{ price: 9, sold: 2 }
];
Then rather than, say, playerBSold, you can use players[1].sold. Now you can use a variable in place of that 1 if you wanted.
Just take a close look at your code you are setting value to array.you are replacing arr[0][1] item value.previously it was playerASold i.e 0 and now 200.So you are not assigning value to playerSold.do like this:
var arr=[[playerAPrice:0,playerASold:0],[playerBPrice:0,playerBSold:0], [playerCPrice:0,playerCSold:0]];
and use this:
arr[0].playerASold=200.
Javascript primitives (in this case Number) are immutable. I.e., you can't change their values. Operations on Numbers create new numbers. Objects are however mutable.
As icktoofay suggested, refactoring as mutable Player objects with price and sold properties is probably a good idea here.