underscorejs pluck two dimensional array - javascript

First time using underscore and I am stuck and cannot find an example.
My data is:
[{
"store_name": "Store 1",
"franchisee_id": "id01",
"dish_menu": "Breakfast",
"dish_count": "17"
}, {
"store_name": "Store 1",
"franchisee_id": "id01",
"dish_menu": "Light Meals",
"dish_count": "7"
}, {
"store_name": "Store 1",
"franchisee_id": "id01",
"dish_menu": "Sandwiches",
"dish_count": "12"
}, {
"store_name": "Store 2",
"franchisee_id": "id02",
"dish_menu": "Breakfast",
"dish_count": "7"
},
............
]
I have managed (with some help from here) to pull the distinct store_name with the following chained command and then place it into a HTML statement I am building:
var stores = _.chain(json).pluck("store_name").sort().uniq(true).value();
var tempHTML = "";
stores.forEach(function (entry) {
tempHTML = tempHTML + '<option value="' + entry + '">' + entry + '</option>';
});
But I am trying to match the franchisee_id to the distinct store_name and essentially build my HTML like below:
stores.forEach(function (entry) {
tempHTML = tempHTML + '<option value="' + FRANCHISEE_ID + '">' + STORE_NAME + '</option>';
});
Is there a way to _.pluck a value for franchisee_id using the store_name value? There is a 1:1 relationship between these two fields so even getting the "first found" franchisee_id is fine. Thanks!

You could do something like this to get your id/name pairs in the desired order:
var map_id_to_name = function(m, o) {
m[o.franchisee_id] = o.store_name;
return m;
};
var arrayify = function(name, id) {
return [ name, id ];
};
var stores = _(data).chain()
.reduce(map_id_to_name, { })
.map(arrayify)
.sortBy(_.first)
.value();
Demo: http://jsfiddle.net/ambiguous/9xxS6/
That will give you an array of arrays in stores that you can spin through to build your <option>s; stores will look like this:
[
[ "Store 1", "id01" ],
[ "Store 2", "id02" ],
...
]
The store name will be in the first entry of the inner arrays and the franchise ID in the second. The whole thing will be sorted by store name. If you want a case insensitive sort then you can .sortBy(function(a) { return a[0].toLowerCase() }) instead.
The reduce(map_id_to_name, { }) collects the unique id/name pairs using an Object to automatically enforce uniqueness (the keys in an Object are unique after all). Then map(arrayify) converts the Object to an array-of-arrays so that you can sort things. You could use an array-of-objects instead, that would just be a small alteration to the map and sortBy calls.

A different approach, extracting the desired information from the objects and then filtering the resulting array to get unique objects:
var stores = _(data).chain()
// convert each object to {store_name: ..., franchisee_id: ...}
.map(function(m) {
return _.pick(m, "store_name", "franchisee_id");
})
//keep one of each
//can be shortened to .uniq(_.property('franchisee_id'))
.uniq(function(m) {
return m.franchisee_id;
})
//sort by name
.sortBy("store_name")
//and get the array
.value();
Your final array would look like
[
{store_name: "Store 1", franchisee_id: "id01"},
{store_name: "Store 2", franchisee_id: "id02"}
]
And a demo to play with http://jsfiddle.net/mr82s/
And with a little _.mixin magic, you could further condense it to
_.mixin({
properties: function() {
var args = _.toArray(arguments);
return function(obj) {
return _.pick(obj, args);
};
}
});
var stores = _(data).chain()
.map(_.properties("store_name", "franchisee_id"))
.uniq(_.property('franchisee_id'))
.sortBy("store_name")
.value()
http://jsfiddle.net/nikoshr/mr82s/1/

Related

javascript array push keeps repeating it self cant find out why

I cant figure out why this arrayUnd gets repeated key values. ive been on this for three weeks. I must be doing somehthing stupid. I know they are loops. but everything works except the push. it logs when the key repeats and stuff. but it somehow is adding it to it? very confusing.
Heres my Javascript
var array = [
{"size":["12","22"]},
{"color":["blue"]},
{"design":["flower-blue"]},
{"size":["12","22","44"]},
{"color":["red"]},
{"design":["flower-blue"]}
]
//output array
arrayUnd=[
{"color":["red"]}
]
//is array?
console.log( Array.isArray(array))
function pusher(obj){
arrayUnd.push(obj)
}
function isRepeat(key,value,obj){
// console.log(key[0])
for (let item=0; item < arrayUnd.length; item++ ){
if ( arrayUnd[item].hasOwnProperty(key)){
console.log("key: " + key)
console.log("yes")
console.log(arrayUnd)
}
else{
console.log("keyno: " + key)
console.log("no")
//pusher(obj)
if ( arrayUnd[item].hasOwnProperty(key) === false){
pusher(obj)
}
console.log(arrayUnd)
}
}
}
array.forEach((obj)=>{
var a= Object.keys(obj)
var b= Object.values(obj)
isRepeat(a,b,obj)
})
console.log(arrayUnd)
Your loop is checking all objects to see if they don't match. At least one of them won't match, so it will try to push that many times. You need to check all elements, and after checking them all for existence decide if you want to push or not.
Current logic is equivalent to: if anything in arrUnd doesn't match, push me each time I check.
some works here, because it checks if anything matches, and returns true or false, which you can then use to decide if you want to push or not (only once, after I've found if anything in the array matches, deciding using the final result).
Using some to check if any other element with same key exists. Push if nothing found.
var array = [{
"size": ["12", "22"]
},
{
"color": ["blue"]
},
{
"design": ["flower-blue"]
},
{
"size": ["12", "22", "44"]
},
{
"color": ["red"]
},
{
"design": ["flower-blue"]
}
]
//output array
arrayUnd = [{
"color": ["red"]
}]
//is array?
console.log(Array.isArray(array))
function pusher(obj) {
arrayUnd.push(obj)
}
function isRepeat(key, value, obj) {
if(!arrayUnd.some(x => x.hasOwnProperty(key[0])))
arrayUnd.push(obj)
}
array.forEach((obj) => {
var a = Object.keys(obj)
var b = Object.values(obj)
isRepeat(a, b, obj)
})
console.log(arrayUnd)
You are passing an array to isRepeat instead of the key and value of the object.
Object.keys() returns an array, even if the object only has one key.
When you check if ( arrayUnd[item].hasOwnProperty(key) === false), arrayUnd[item].hasOwnProperty(key) will always be false, so the object will always get pushed to your array.
You can fix this by accessing the first key and value of each object:
array.forEach((obj)=>{
var a= Object.keys(obj)
var b= Object.values(obj)
isRepeat(a[0],b[0],obj)
})
The reason your code keeps pushing object to arrayUnd it's because when it iterates through the array it checks for the array key if its present if not it pushes it to arrayUnd, now you have 2 problems first you are not actually checking for the array key to match you comparing object so you will always get false , and second is that each time you push to the array the length of your array grow and so the number of iterations increases
you can achieve this in two lines of code
var array = [
{"size":["12","22"]},
{"color":["blue"]},
{"design":["flower-blue"]},
{"size":["12","22","44"]},
{"color":["red"]},
{"design":["flower-blue"]}
]
//output array
arrayUnd=[
{"color":["red"]}
]
array.forEach(p=>Object.entries(p).forEach(p=>{
!arrayUnd.some(o=>o.hasOwnProperty(p[0])) ? arrayUnd.push({[p[0]]:p[1]}):null
}))
console.log(arrayUnd)
var inpAry = [
{ "size": ["12", "22"] },
{ "color": ["blue"] },
{ "design": ["flower-blue"] },
{ "size": ["12", "22", "44"] },
{ "color": ["red"] },
{ "design": ["flower-blue"] }
];
var opAry = [
{ "color": ["red"] }
];
inpAry.forEach(inpAryElem => {
var ipAryElemKeys = Object.keys(inpAryElem);
var ipAryElemVals = Object.values(inpAryElem);
ipAryElemKeys.forEach((ipAryElmKey,ipAryElemKyIdx) => {
var isKeyPresent = false;
opAry.forEach(opAryElem => {
if(opAryElem[ipAryElmKey]) {
isKeyPresent = true;
opAryElem[ipAryElmKey].push(...ipAryElemVals[ipAryElemKyIdx]);
}
});
if(!isKeyPresent) {
opAry.push({[ipAryElmKey]:ipAryElemVals[ipAryElemKyIdx]});
}
})
});
console.log(opAry);

React - Filter JSON array if key exists [duplicate]

I have an array of objects and I'm wondering the best way to search it. Given the below example how can I search for name = "Joe" and age < 30? Is there anything jQuery can help with or do I have to brute force this search myself?
var names = new Array();
var object = { name : "Joe", age:20, email: "joe#hotmail.com"};
names.push(object);
object = { name : "Mike", age:50, email: "mike#hotmail.com"};
names.push(object);
object = { name : "Joe", age:45, email: "mike#hotmail.com"};
names.push(object);
A modern solution with Array.prototype.filter():
const found_names = names.filter(v => v.name === "Joe" && v.age < 30);
Or if you still use jQuery, you may use jQuery.grep():
var found_names = $.grep(names, function(v) {
return v.name === "Joe" && v.age < 30;
});
You can do this very easily with the [].filter method:
var filterednames = names.filter(function(obj) {
return (obj.name === "Joe") && (obj.age < 30);
});
You can learn more about it on this MDN page.
You could utilize jQuery.filter() function to return elements from a subset of the matching elements.
var names = [
{ name : "Joe", age:20, email: "joe#hotmail.com"},
{ name : "Mike", age:50, email: "mike#hotmail.com"},
{ name : "Joe", age:45, email: "mike#hotmail.com"}
];
var filteredNames = $(names).filter(function( idx ) {
return names[idx].name === "Joe" && names[idx].age < 30;
});
$(filteredNames).each(function(){
$('#output').append(this.name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"/>
var nameList = [
{name:'x', age:20, email:'x#email.com'},
{name:'y', age:60, email:'y#email.com'},
{name:'Joe', age:22, email:'joe#email.com'},
{name:'Abc', age:40, email:'abc#email.com'}
];
var filteredValue = nameList.filter(function (item) {
return item.name == "Joe" && item.age < 30;
});
//To See Output Result as Array
console.log(JSON.stringify(filteredValue));
You can simply use javascript :)
For those who want to filter from an array of objects using any key:
function filterItems(items, searchVal) {
return items.filter((item) => Object.values(item).includes(searchVal));
}
let data = [
{ "name": "apple", "type": "fruit", "id": 123234 },
{ "name": "cat", "type": "animal", "id": 98989 },
{ "name": "something", "type": "other", "id": 656565 }]
console.log("Filtered by name: ", filterItems(data, "apple"));
console.log("Filtered by type: ", filterItems(data, "animal"));
console.log("Filtered by id: ", filterItems(data, 656565));
filter from an array of the JSON objects:**
var names = [{
name: "Joe",
age: 20,
email: "joe#hotmail.com"
},
{
name: "Mike",
age: 50,
email: "mike#hotmail.com"
},
{
name: "Joe",
age: 45,
email: "mike#hotmail.com"
}
];
const res = _.filter(names, (name) => {
return name.name == "Joe" && name.age < 30;
});
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
So quick question. What if you have two arrays of objects and you would like to 'align' these object arrays so that you can make sure each array's objects are in the order as the other array's? What if you don't know what keys and values any of the objects inside of the arrays contains... Much less what order they're even in?
So you need a 'WildCard Expression' for your [].filter, [].map, etc. How do you get a wild card expression?
var jux = (function(){
'use strict';
function wildExp(obj){
var keysCrude = Object.keys(obj),
keysA = ('a["' + keysCrude.join('"], a["') + '"]').split(', '),
keysB = ('b["' + keysCrude.join('"], b["') + '"]').split(', '),
keys = [].concat(keysA, keysB)
.sort(function(a, b){ return a.substring(1, a.length) > b.substring(1, b.length); });
var exp = keys.join('').split(']b').join('] > b').split(']a').join('] || a');
return exp;
}
return {
sort: wildExp
};
})();
var sortKeys = {
k: 'v',
key: 'val',
n: 'p',
name: 'param'
};
var objArray = [
{
k: 'z',
key: 'g',
n: 'a',
name: 'b'
},
{
k: 'y',
key: 'h',
n: 'b',
name: 't'
},
{
k: 'x',
key: 'o',
n: 'a',
name: 'c'
}
];
var exp = jux.sort(sortKeys);
console.log('#juxSort Expression:', exp);
console.log('#juxSort:', objArray.sort(function(a, b){
return eval(exp);
}));
You can also use this function over an iteration for each object to create a better collective expression for all of the keys in each of your objects, and then filter your array that way.
This is a small snippet from the API Juxtapose which I have almost complete, which does this, object equality with exemptions, object unities, and array condensation. If these are things you need or want for your project please comment and I'll make the lib accessible sooner than later.
Hope this helps! Happy coding :)
The most straightforward and readable approach will be the usage of native javascript filter method.
Native javaScript filter takes a declarative approach in filtering array elements. Since it is a method defined on Array.prototype, it iterates on a provided array and invokes a callback on it. This callback, which acts as our filtering function, takes three parameters:
element — the current item in the array being iterated over
index — the index or location of the current element in the array that is being iterated over
array — the original array that the filter method was applied on
Let’s use this filter method in an example. Note that the filter can be applied on any sort of array. In this example, we are going to filter an array of objects based on an object property.
An example of filtering an array of objects based on object properties could look something like this:
// Please do not hate me for bashing on pizza and burgers.
// and FYI, I totally made up the healthMetric param :)
let foods = [
{ type: "pizza", healthMetric: 25 },
{ type: "burger", healthMetric: 10 },
{ type: "salad", healthMetric: 60 },
{ type: "apple", healthMetric: 82 }
];
let isHealthy = food => food.healthMetric >= 50;
const result = foods.filter(isHealthy);
console.log(result.map(food => food.type));
// Result: ['salad', 'apple']
To learn more about filtering arrays in functions and yo build your own filtering, check out this article:
https://medium.com/better-programming/build-your-own-filter-e88ba0dcbfae

Javascript - Create and populate associative array containing sub arrays

I'm trying to collate some data. I would like to populate an array containing sub arrays, for example, I have some json data that I am iterating over:
{
"name": "name1",
"prices": "209.67"
},
{
"name": "name1",
"prices": "350"
},
{
"name": "name2",
"price": "195.97"
},
I would like to create an array that ends up looking something like the following:
myArray['name1']prices[0] = 209.67,
prices[1] = 350,
['name2']prices[0] = 195.97
I thought that the code below would achieve what I wanted but it doesn't work. It throws an exception. It doesn't seem to recognise the fact that the prices are an array for a given index into the main array. Instead the prices appear at the same level as the names. I want the main array for a given name to contain an inner array of prices.. Does anybody have any idea how I could modify to make this work?
function doStuff() {
var cryptoData = getData();
var datasetValues = {};
datasetValues.names = [];
datasetValues.names.prices = [];
for (var result = 0; result < cryptoData.length; result++) {
var data = cryptoData[result];
if (datasetValues.names.indexOf(data.cryptoname) === -1)
{
datasetValues.names.push(data.cryptoname);
}
// This works
//datasetValues.names.prices.push(data.prices);
// This doesn't!
datasetValues.cryptoNames[data.cryptoname].prices.push(data.prices);
}
}
You could reduce the array by using an object and take a default object if the property is not set. Then push the price.
var data = [{ name: "name1", price: "209.67" }, { name: "name1", price: "350" }, { name: "name2", price: "195.97" }],
result = data.reduce((r, { name, price }) => {
r[name] = r[name] || { name, prices: [] };
r[name].prices.push(+price);
return r;
}, Object.create(null));
console.log(result);
Try this
function parseData(input){
return input.reduce(function(o,i){
o[i.name] = {};
if(!o[i.name]['prices']){
o[i.name]['prices'] = [];
}
o[i.name]['prices'].push(i.prices);
return o;
},{});
}

How can I get data iterate Json?

I need get data the my Json but I can't use 'key' because the 'key' is different each day.
I tried :
template: function(params) {
const objects = JSON.parse(JSON.stringify(params.data.masterdetail));
for (const obj of objects) {
const keys = Object.keys(obj);
const cont = 0;
keys.forEach(key => {
const valor = obj[key];
console.log('value ', valor[0]);
});
}
I first tried with 0 and then use cont, but with 0 console.log (value is undefined)....
If I use console.log ('value' , valor['name']) IT'S OK ! but I can't use keys and if I use valor[0] is undefined...........
Example Json
{
"headers": [
"headerName": "asdasd",
], //end headers
"datas": [
"idaam": "11",
"idorigen": "11",
"masterdetail": [{
"child1": {
"name": "I AM",
"age": "1"
},
"child2": {
"name": "YOU ARE",
"age": "2"
},
"child3": {
"name": "HE IS",
"age": "3"
},
}] //end masterdetail
]//end datas
}//end JSON
Edit :
I can't use 'keys' because today I receive "name", "typeval" etc. and tomorrow I can get 'surname','id' etc.
If you see in my first img you can see "4" bits of data.
1º obj[key]{
name = "adopt",
typeval= "",
etc
}
2º obj[key]{
"link" = "enlace",
"map" = "map"
etc
}
If I use this code : I get "name" OKEY but
I HAVE PROHIBITED use of value['name'] or value[typeval] because this Json always is dynamic.
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
let value;
keys.forEach(key => {
value = objects[key];
console.log(value['name']);
console.log(value['typeval']);
});
I need for example :
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
cont = 0 ;
keys.forEach(key => {
value = objects[key];
console.log(value[0]);
});
but value[0] is undefined and then when I arrive 2ºobj[key] link is index 0 but cont maybe is .... 4...
Sorry for my English...
To simply print the objects within the first entry in the masterdetail array, you can do the following:
var objects = params.datas.masterdetail[0];
const keys = Object.keys(objects);
keys.forEach(key => {
console.log('value ', objects[key]);
});
Based on a (suitably corrected - see my comments above) version of the JSON above, this would produce console output as follows:
value {name: "I AM", age: "1"}
value {name: "YOU ARE", age: "2"}
value {name: "HE IS", age: "3"}
Unfortunately it's not 100% clear from the question if this is the output you were looking for, but that's my best guess based on the code.
Your main mistakes were that
1) masterdetail is an array, and all the data is within the first element of that array, so to get the objects within it you need to select that element first. If the array can have multiple elements in real life then you'd need an outer loop around the code above to iterate through it.
2) If you're looping through the keys of an object, you don't need to also iterate through the properties a different way. You seemed to have two loops designed to do the same thing.

how to get unique values from array of objects with addition of numbers

I'm working in wso2 carbon dashboard. My table is containing 2 fields (Name and Number). I Have duplicate name in the objects but with different number. I want unique name with addition of numbers.
[
{
"Name":"Abc",
"number":2
},
{
"Name":"bcd",
"number":3
},
{
"Name":"Abc",
"number":5
}
]
expected output
[
{
"name":"Abc",
"Number":7
},
{
"name":"bcd",
"Number":3
}
]
I'm using java script to achieve such task. please help me
Use Array#reduce method with a reference object.
var data = [{
"Name": "Abc",
"number": 2
}, {
"Name": "bcd",
"number": 3
}, {
"Name": "Abc",
"number": 5
}];
// object for index reference
var ref = {};
// iterate and generate the array
var res = data.reduce(function(arr, o) {
// check index already defined
if (!(o.Name in ref)) {
// if not defined then define index
ref[o.Name] = arr.length;
// and push the element
// you can also use
// arr.push({Name:o.Name, number:o.number});
arr.push(Object.assign({}, o));
} else {
// if index already defined update the number
arr[ref[o.Name]].number += o.number;
}
// return the array reference
return arr;
// set initial value as empty array
}, []);
console.log(res);

Categories

Resources