I have arrays like this,
0: {"pure_id":"1","branch_id":"45"}
1: {"pure_id":"2","branch_id":"45"}
2: {"pure_id":"3","branch_id":"45"}
3: {"pure_id":"3","branch_id":"45"}
I am looking to group the above arrays into a single array based on pure_id, so the result array will be this below, and the index will be pure_id index
1: [{"pure_id":"1","branch_id":"45"}]
2: [{"pure_id":"2","branch_id":"45"}]
3: [{"pure_id":"3","branch_id":"45"},{"pure_id":"3","branch_id":"45"}]
I am trying lots of things honestly, but I cannot make it work.
Please help.
const result = [];
for(const el of array)
(result[el.pure_id] || (result[el.pure_id] = [])).push(el);
Using the new object literal rest and spread syntax, the property expression syntax, and .reduce() makes this pretty short and simple.
var arr = [
{"pure_id":"1","branch_id":"45"},
{"pure_id":"2","branch_id":"45"},
{"pure_id":"3","branch_id":"45"},
{"pure_id":"3","branch_id":"45"}
];
var result = arr.reduce((res, {pure_id, ...props}) =>
({...res, [pure_id]: (res[pure_id] || []).concat({pure_id, ...props})})
, {});
console.log(result);
The .reduce() is of course just creating a new object. Each iteration takes the accumulated properties and adds them to an object literal, but with the pure_id being the concatenation of the properties of the current object (copy) to an array.
I often see people struggle with something in programming that outside of it would not be a problem. Instead of arrays and objects, you can think of it as fruit.
Imagine a bucket of fruit. You want to sort the one bucket of fruit into 1 bucket for each color of fruit (color just being a property of a piece of fruit). So you would:
grab one fruit at a time
figure out it's color
if there is no bucket for that color, get one
put the fruit in the proper bucket
repeat until there are no fruits left
What we created was an algorithm for sorting fruit by color. The problem you are solving is very similar. In pseudocode it would be:
for each fruit
color = fruit.color
if not bucket[color]
make bucket[color]
put fruit in bucket[color]
to translate that to JS
var fruits = [
{color: "red"},
{color: "orange"},
{color: "pink"},
{color: "red"}
];
var buckets = {};
for (var i=0; i<fruits.length; i++) {
var fruit = fruits[i];
var color = fruit.color;
if (!buckets[color])
buckets[color] = [];
buckets[color].push(fruit);
}
So now, if you want to work with your array, the algorithm stays the same you just swap out the parts.
var things = [
{"pure_id":"1","branch_id":"45"},
{"pure_id":"2","branch_id":"45"},
{"pure_id":"3","branch_id":"45"},
{"pure_id":"3","branch_id":"45"}
];
var grouped = [];
for (let i=0; i<things.length; i++) {
var thing = things[i];
var id = thing['pure_id'];
if (!grouped[id])
grouped[id] = [];
grouped[id].push(thing);
}
console.log(grouped)
So, here we are at the first solution. It works! There are better solutions (using reduce, forEach or others) but don't let data structures like arrays or dictionaries scare you away. Think about it in simpler terms if it helps.
If don't use reduce, the codes will be like below:
Assuming your expected result is one array like:
[[{'pure_id':0, 'branch_id':1}], [{'pure_id':1, 'branch_id':2}]]
//assuming your array is like below:
var test = [{"pure_id":"1","branch_id":"43"},
{"pure_id":"2","branch_id":"44"},
{"pure_id":"3","branch_id":"45"},
{"pure_id":"3","branch_id":"46"}]
var result = []
var index_db = {} //use dict to save the mapping between the index of result and pure_id
var index = 0
test.forEach(function(item){
if(item.pure_id in index_db){
result[index_db[item.pure_id]].push(item)
}
else{
result.push([item])
index_db[item.pure_id] = index++
}
});
console.log(result)
Below is one reduce version:
//assuming your array is like below:
var test = [{"pure_id":"1","branch_id":"43"},
{"pure_id":"2","branch_id":"44"},
{"pure_id":"3","branch_id":"45"},
{"pure_id":"3","branch_id":"46"}]
result = test.reduce(function(pre, cur){
if(cur['pure_id'] in pre['indexes']){
pre['result'][pre['indexes'][cur['pure_id']]].push(cur)
}
else{
pre['result'].push([cur])
pre['indexes'][cur['pure_id']] = pre['pos']++
}
return pre
}, {'pos':0, 'indexes':{}, 'result':[]})
console.log(result['result'])
Related
I've below Json object that contain car associated to their manufacturer. From Json object, I want to get total number of cars as well as occurrence of a specific car in the object.
var Cars = { "manufacturer":"Car":
[{"Saab":"Automobile AB"},
{"Volvo":"V40"},
{"BMW":"Estoril Blue"},
{"Volvo":"V40"},
]};
I tried to use the filter but since filter is only specific for Arrays so cannot used it with Json object. Below is the source code.
var Cars = {"manufacturer":"Car":
[{"Saab":"Automobile AB"},
{"Volvo":"V40"},
{"BMW":"Estoril Blue"},
{"Volvo":"V40"},
]};
var volvo = "V40";
var numberOfCars = Cars.filter(function (x) {
return x === volvo;
}).length;
I expect the output of the above source code as 2. But I get an exception
Cars.filter is not a function
I need you guys to please help me to get the occurrence of V40 (that is 2), as well as total number of cars (which are 4).
It's easy enough to walk over the properties of an object. One way is to use Object.entries to get an array of the properties and values;
var Cars = [{"Saab":"Automobile AB"}, {"Volvo":"V40"}, {"BMW":"Estorill Blue"}, {"Volvo":"V40"}];
let properties = Object.entries(Cars);
console.log("Number of cars: ", Cars.length);
console.log("Number of Volvos: ", Cars.filter((car) => Object.keys(car)[0] === "Volvo").length);
Use filter and then you can create object which has the duplicate car name as key and its number of occurences as val.
const input = [{"Saab":"Automobile AB"}, {"Volvo":"V40"}, {"BMW":"Estorill Blue"}, {"Volvo":"V40"}];
const volvo = "V40";
const duplicateCars = input.filter(({Volvo}) => volvo == Volvo);
const duplicateCarName = Object.keys(duplicateCars[0])[0];
console.log({[duplicateCarName]:duplicateCars.length});
How to split an object into array of objects based on a condition.
oldObject = {"Chicago, IL:Myrtle Beach, SC": 0.005340186908091907,
"Portsmouth, NH:Rock Hill, SC": 0.0063224791225441205,
"Columbia, SC:Laconia, NH": 0.006360767389277389,
"Council Bluffs, IA:Derry, NH": 0.0016636141225441225}
Above is the given sample object. I want to make an array of objects like this,
newArray = [{"city":"Chicago", "similarTo":"Myrtle"},
{"city":"Portsmouth", "similarTo":"Rock Hill"},
{"city":"Columbia", "similarTo":"Laconia"},
{"city":"Council Bluffs", "similarTo":"Derry"}]
I have been scratching my head with this for a while now. How can I get the above array(newArray)?
Here is a bunch of code you can try.
1) Iterate over oldObject and get the name of the property.
2) Split that name into an array based on the ":" character, since it separates the cities
3) Go over that new array, splitting it on the "," character (so as not to get the states).
4) Put the values into the newObject, based on whether it's the first or second part of the original property name.
5) Push that newObject, now with items, into a newArray.
Basically, this parses apart the name and does some array splitting to get at the right values. Hope it helps and helps you understand too.
var oldObject = {"Chicago, IL:Myrtle Beach, SC": 0.005340186908091907,
"Portsmouth, NH:Rock Hill, SC": 0.0063224791225441205,
"Columbia, SC:Laconia, NH": 0.006360767389277389,
"Council Bluffs, IA:Derry, NH": 0.0016636141225441225};
var newArray = [];
for (object in oldObject) {
var thisObjectName = object;
var thisObjectAsArray = thisObjectName.split(':');
var newObject = {
'city': '',
'similar_to': ''
};
thisObjectAsArray.forEach(function(element,index,array) {
var thisObjectNameAsArray = element.split(',');
var thisObjectNameCity = thisObjectNameAsArray[0];
if(index===0) {
newObject.city = thisObjectNameCity;
} else if(index===1) {
newObject.similar_to = thisObjectNameCity;
}
});
newArray.push(newObject);
}
console.log(newArray);
PS: to test, run the above code and check your Developer Tools console to see the new array output.
I have an array of objects that looks like this:
[
{"name":"Andrea","from":"USA","Food":"Candy"},
{"name":"Matt","from":"Taiwan","Food":"Chicken"},
{"name":"Roddy","from":"USA","Food":"Rice"},
{"name":"Andy","from":"Great Britain","Food":"Steak"},
];
Is there a way to get the list of all countries from the array above, and get rid of the repeated ones?
So from the list above, the list I am to obtain is:
["USA", "Taiwan", "Great Britain"]
Thank you!
Just loop over people and insert unique countries in a new array. Here is an example.
var countries = [];
var people = [
{"name":"Andrea","from":"USA","Food":"Candy"},
{"name":"Matt","from":"Taiwan","Food":"Chicken"},
{"name":"Roddy","from":"USA","Food":"Rice"},
{"name":"Andy","from":"Great Britain","Food":"Steak"},
];
for (var i = 0, l=people.length; i < l; i++) {
if(people[i] && people[i].from) {//ensure country exists
if (countries.indexOf(people[i].from) == -1) {//ensure unique
countries.push(people[i].from);
}
}
}
Yet another variant with reduce
var arr = [
{"name":"Andrea","from":"USA","Food":"Candy"},
{"name":"Matt","from":"Taiwan","Food":"Chicken"},
{"name":"Roddy","from":"USA","Food":"Rice"},
{"name":"Andy","from":"Great Britain","Food":"Steak"},
];
var countries = arr.reduce(function(acc, cur){
if(!acc.map[cur.from]){
acc.map[cur.from]=true;
acc.result.push(cur.from);
}
return acc;
}, {result:[], map:{}}).result;
var arr = [
{"name":"Andrea","from":"USA","Food":"Candy"},
{"name":"Matt","from":"Taiwan","Food":"Chicken"},
{"name":"Roddy","from":"USA","Food":"Rice"},
{"name":"Andy","from":"Great Britain","Food":"Steak"},
];
var countries = arr.reduce(function(acc, cur){
if(!acc.map[cur.from]){
acc.map[cur.from]=true;
acc.result.push(cur.from);
}
return acc;
}, {result:[], map:{}}).result;
document.getElementById('countries').innerHTML = countries.join();
<span id="countries"></span>
If you are already using the excellent Lodash library, the following will do it for you neatly in one line:
var uniqueCountries = _(dataArray).pluck('from').unique().value();
UnderscoreJS has similar functionality using chaining.
For D3.js, the following will do it:
var uniqueCountries = d3.set(dataArray.map(function (x) { return x.from; })).values();
Without doing the unique-ifying on the server and returning that data separately, there is no way to get around looping through all records at least once to do this. For 1000 records or so, though, this will still be very fast.
For plain JS, see other answers.
I'd loop over the Array and put the country into an array if it is not yet inside that array.
I have an object which comes back as part of a return data from a REST server. It is part of an item object.
(I don't have control over the REST server so I can't change the data received):
{
"Option:Color":"Red,Green,Blue,Orange",
"Option:Size":"Small,Medium,Large"
}
What I want to end up with is some control over this, so that I can display the results when a product is selected in my app. It will appear in a modal. I am using Marionette/Backbone/Underscore/JQuery etc. but this is more of a JavaScript question.
I have tried multiple ways of getting at the data with no success. I would like to be able to have the options in a nested array, but I'd be open to other suggestions...
Basically this kind of structure
var Color=('Red', 'Green', 'Blue', 'Orange')
var Size('Small', 'Medium', 'Large')
The Object structure is fine, just need to be able to translate it to an array and take out the 'Option' keyword
Important to mention that I have no idea what the different options might be when I receive them - the bit after Options: might be any form of variation, color, size, flavour etc.
Loop through the parsed JSON and create new keys on a new object. That way you don't have to create the var names yourself; it's automatically done for you, albeit as keys in a new object.
var obj = {
"Option:Color":"Red,Green,Blue,Orange",
"Option:Size":"Small,Medium,Large"
}
function processObj() {
var newObj = {};
for (var k in obj) {
var key = k.split(':')[1].toLowerCase();
var values = obj[k].split(',');
newObj[key] = values;
}
return newObj;
}
var processedObj = processObj(obj);
for (var k in processedObj) {
console.log(k, processedObj[k])
// color ["Red", "Green", "Blue", "Orange"], size ["Small", "Medium", "Large"]
}
Edit: OP I've updated the code here and in the jsfiddle to show you how to loop over the new object to get the keys/values.
Fiddle.
var json = {
"Option:Color":"Red,Green,Blue,Orange",
"Option:Size":"Small,Medium,Large"
};
var color = json['Option:Color'].split(',');
var size = json['Option:Size'].split(',');
Try this to do get a solution without hardcoding all the option names into your code:
var x = {
"Option:Color":"Red,Green,Blue,Orange",
"Option:Size":"Small,Medium,Large"
};
var clean = {};
$.each(x, function(key, val){ //iterate over the options you have in your initial object
var optname = key.replace('Option:', ''); //remove the option marker
clean[optname] = val.split(","); //add an array to your object named like your option, splitted by comma
});
clean will contain the option arrays you want to create
EDIT: Okay, how you get the names of your object properties like "color", which are now the keys in your new object? Thats the same like before, basically:
$.each(clean, function(key, val){
//key is the name of your option here
//val is the array of properties for your option here
console.log(key, val);
});
Of course we stick to jQuery again. ;)
var Animals = {
"Europe": { "weasel.jpg": "squeak", "cow.jpg": "moo"},
"Africa": { "lion.jpg": "roar", "gazelle.jpg": "bark"},
};
function region(a){
var b = "Animals."+a;
for(var index in b) {
var target = document.getElementById('div1');
var newnode = document.createElement('img');
newnode.src = index;
target.appendChild(newnode)
}
}
RELEVANT HTML
<li onclick="europe('Europe')">Europe</li>
Goal: on the click of the Europe <li>, pass the word Europe into my region function where it is then concatenated to produce Animals.Europe
This is in order to identify an array within the object structure at the top using the for(var index in Animals.Europe) loop. Why is the concatenation which produces Animals.Europe not treated in the same way as if I had typed this out?
In addition, you can see that I have used arrays to store an image source and description for different animals. Using my limited coding knowledge this was all I could think of. Is there an easier way to store image/description data in order to produce in HTML?
"Animals." + a is just a string value, e.g. "Animals.Europe", which is not the same thing as Animals.Europe. If you change the first line to var b = Animals[a];, you should be all set.
Edit: and as elclanrs pointed out, it should be region('Europe'), not europe('Europe').
Why is the concatenation which produces Animals.Europe not treated in the same way as if i had typed this out?
In this case the variable b is just a string ("Animals.Europe"), which is treated like any other string (i.e. a list of characters). This means that when you attempt to loop through it (for(index in b)) you will be looping over a simple list of characters.
What you can do instead is use the square brace notation of accessing an objects properties. This means you can instead write var b = Animals[a], retrieving attribute a from Animals. You can read more about working with objects in this way on this MDN page
You can access the europe property using the following
Animals[a]
Also you're calling a "europe" function when you should be calling "region"
You're not storing animals in arrays here, but in objects with the image names as keys. Usually you'll want to use relevant names as keys. For example if you want arrays of animals for each continent
var Animals = {
"Europe": [{
imageSrc: "weasel.jpg",
cry: "squeak"
},{
imageSrc: "cow.jpg",
cry: "moo"
}],
"Africa": [{
imageSrc: "lion.jpg",
cry: "roar"
},{
imageSrc: "gazelle.jpg",
cry: "bark"
}]
};
Now Animals['Europe'] gives an array of objects, where you could eventually store other properties. So if b is an array your loop will now look like:
var b = Animals['Europe'];
for(var i=0; i < b.length; i++) {
var target = document.getElementById('div1');
var newnode = document.createElement('img');
var animalData = b[i]; // The array item is now an object
newnode.src = animalData.imageSrc;
target.appendChild(newnode)
}