I have an array of cars like this:
[{ name:"Toyota Minivan", id:"506" }, { name:"Honda Civic", id:"619" }]
I am trying to check whether the array contains a certain id.
I have tried
var x =!!_.where(cars, {id:'506'}).length;
expecting it to return true if the array contains the id, but it always returns false.
What am I doing here ?
Btw, I don't have to use underscore.js if there is a better way of doing this.
thanks
Thomas
Your code does work (once you fix the syntax errors in the object array):
http://jsfiddle.net/ArPCa/
var cars = [{ name:"Toyota Minivan", id:"506"}, { name:"Honda Civic", id:"619"}];
var x =!!_.where(cars, {id:'506'}).length;
console.log('value: ' + x);
returns "value: true". So there must be a problem somewhere else.
But, a better way to do this might be some:
var y = _.some(cars, function(c) {
return c.id == '506';
});
I know this is late, but even easier:
var cars = [{ name:"Toyota Minivan", id:"506"}, { name:"Honda Civic", id:"619"}];
function findById(id) {
return _.contains(_.pluck(cars, 'id'), id);
}
Say you have the array arr, and your id, id.
arr.filter(function(elem) { return elem.id == id; });
will return an array with the matching element(s);
You could wrap it in a function like:
function findById(arr, id) {
var filtered = arr.filter(function(elem) { return elem.id == id; });
return filtered && filtered.length ? filtered[0] : null;
}
, potentially doing some other stuff if you weren't happy with the default filtered array.
var obj = [{
name: "Toyota Minivan",
id: "506"
}, {
name: "Honda Civic",
id: "619"
}];
function findCar(id) {
return obj.filter(function (car) {
if (car.id == id) {
return car;
}
});
}
Related
I have an array of objects which store the data of different 'pupils'.
var pupils = [
{
id: 0,
name: 'will'
},
{
id: 1,
name: 'megan'
}
];
I want to create a function called 'findPupil' which takes three parameters: the property you know, the value you know it to be and the property you want to find.
Say you know that the pupil you are looking for has an id of 1 but you don't know their name. In this case, you would call the function like this:
var name = findPupil('id', 1, 'name'); // should now store 'Megan' as a string
Here is the function I have written:
function findPupil(property, value, find) {
pupils.forEach(function(pupil) {
if(pupils[pupil][`${property}`] === value) {
return pupils[pupil][`${find}`];
}
});
}
Calling this function returns the following:
Error: Cannot read property 'id' of undefined
How do I make this function work?
Use Array.find() to find an object with the property you know, or a default if an object is not found. Extract the find property from the object:
const pupils = [{"id":0,"name":"will"},{"id":1,"name":"megan"}];
const findPupil = (property, value, find) =>
(pupils.find(o => o[property] === value) || {})[find];
const name = findPupil('id', 1, 'name');
console.log(name);
You could use .find() for that.
var pupils = [{id: 0, name: 'will'},{id: 1,name: 'megan'}];
function getByPropertyAndValue(knownProp, knownValue, desiredProperty) {
let pupil = pupils.find(p => p[knownProp] === knownValue); //Find pupil by property and value
return pupil && pupil[desiredProperty]; //Return the value, or undefined if not found
}
console.log(getByPropertyAndValue("id", 1, "name"));
Most of your code is correct except for the part of your if statement. Please find working example -
var pupils = [{
id: 0,
name: 'will'
},
{
id: 1,
name: 'megan'
}
];
function findPupil(property, value, find) {
let result
pupils.forEach(function(pupil) {
if (pupil[property] === value) {
result = pupil[find];
}
});
return result
}
var name = findPupil('id', 1, 'name');
console.log(name)
var pupils = [
{ id: 0, name: 'will' },
{ id: 1, name: 'megan' }
];
function findPupil(property, value, find) {
var find_value = '';
for (var i = 0; i < pupils.length; i++) {
if (find_value == '') {
Object.keys(pupils[i]).forEach(function (key) {
if (key == property && pupils[i][key] == value) {
find_value = pupils[i][find];
}
});
} else {
break;
}
}
console.log(find_value);
return find_value;
}
var name = findPupil('id', 1, 'name');
Javascript:
$(document).ready(function() {
const ores = "../js/json/oreList.json";
const priceURL = "https://esi.tech.ccp.is/latest/markets/prices/?datasource=tranquility";
let oreArray = [];
let priceArray = [];
let total = 0;
// Retrieve list of ores
function getOres() {
$.getJSON(ores, function(ores) {
ores.forEach(function(ore) {
total++;
if (total === 48) {
getPrices();
}
oreArray.push(ore);
});
});
}
// Retrieve all items & prices via API
function getPrices() {
$.getJSON(priceURL, function(prices) {
prices.forEach(function(data) {
priceArray.push(data);
console.log(data);
});
});
}
getOres();
});
The first function creates an internal array from my .JSON file and the second function creates an internal array from the URL.
In the first array oreArray, an object looks like this:
{ id: 1234, name: "Title" }
In the second array priceArray, an object looks like this:
{ type_id: 1234, average_price: 56.34 }
My oreArray has 48 objects and unfortunately the priceArray has about 11,000 objects. I need to create a new array by comparing the two arrays and building new objects, where the ID's match. So for example objects in newArray would look like:
{ id: 1234, name: "Title", average_price: 56.34 }
Basically I'm having trouble figuring out the logic for:
For each object in oreArray, find the object with the same ID value in priceArray and append the new array with a new object using values from both arrays.
I would do it this way:
const ores = "../js/json/oreList.json",
priceURL = "https://esi.tech.ccp.is/latest/markets/prices/?datasource=tranquility";
let oreArray,
priceArray,
joinedArray = [];
function getOres() {
$.getJSON(ores, function(ores) {
oreArray = ores;
getPrices();
});
}
function getPrices() {
$.getJSON(priceURL, function(prices) {
priceArray = prices;
joinPrices();
});
}
function joinPrices() {
oreArray.forEach(function(ore) {
var matchingPrice = getMatchingPrice(ore);
if(matchingPrice !== false) {
joinedArray.push({
id: ore.id,
name: ore.name,
average_price: matchingPrice.average_price
});
}
});
}
function getMatchingPrice(ore) {
for(var i=0; i<priceArray.length; i++) {
if(priceArray[i].type_id === ore.id) {
return priceArray[i];
}
}
return false;
}
getOres();
I think that a good way to approach this problem is by changing the data structure of the average prices a little bit.
Instead of having them in an array, where each item has type_id and average_price field, you might want to consider using an object to store them, where the key is the type_id and the value is the average_price.
To be more concrete, you can replace:
prices.forEach(function(data) {
priceArray.push(data);
});
With:
const pricesMap = {};
prices.forEach(price => {
pricesMap[price.type_id] = price.average_price
});
And when looping on the oreArray, you can access each product's average_price by simply referring to pricesMap[ore.id]
You can check out this JSBin: http://jsbin.com/fogayaqexe/edit?js,console
You can use reduce to loop over each oreArr item and collect the data you need in the accumulator:
var oreArr=[
{ id: 1234, name: "Title" },
{ id: 2234, name: "2Title" },
]
var priceArr= [
{ type_id: 1234, average_price: 56.34 },
{ type_id: 2234, average_price: 256.34 },
{ type_id: 3234, average_price: 56.34 },
{ type_id: 4234, average_price: 56.34 },
]
var resArr = oreArr.reduce((ac,x) => {
var priceMatch = priceArr.find( z => z.type_id === x.id )
if(! priceMatch)
return ac //bail out if no priceMatch found
var res = Object.assign({}, x, priceMatch)
ac.push(res)
return ac
},[])
console.log(resArr)
other methods used:
arrayFind to check intersection
Object.assign to create the merged object to populate the accumulator
I suggest you to change your small json as object
eg : '{"1234":{"id": 1234, "name": "Title" }}';
var json = '{"1234":{"id": 1234, "name": "Title" }}';
oreArray = JSON.parse(json);
alert(oreArray['1234'].name); // oreArray[priceArraySingle.id].name
we can easily match priceArray id with oreArray.
A React component is passed a state property, which is an object of objects:
{
things: {
1: {
name: 'fridge',
attributes: []
},
2: {
name: 'ashtray',
attributes: []
}
}
}
It is also passed (as a router parameter) a name. I want the component to find the matching object in the things object by comparing name values.
To do this I use the filter method:
Object.keys(this.props.things).filter((id) => {
if (this.props.things[id].name === this.props.match.params.name) console.log('found!');
return (this.props.things[id].name === this.props.match.params.name);
});
However this returns undefined. I know the condition works because of my test line (the console.log line), which logs found to the console. Why does the filter method return undefined?
Object.keys returns an array of keys (like maybe ["2"] in your case).
If you are interested in retrieving the matching object, then you really need Object.values. And if you are expecting one result, and not an array of them, then use find instead of filter:
Object.values(this.props.things).find((obj) => {
if (obj.name === this.props.match.params.name) console.log('found!');
return (obj.name === this.props.match.params.name);
});
Be sure to return that result if you use it within a function. Here is a snippet based on the fiddle you provided in comments:
var state = {
things: {
1: {
name: 'fridge',
attributes: []
},
2: {
name: 'ashtray',
attributes: []
}
}
};
var findThing = function(name) {
return Object.values(state.things).find((obj) => {
if (obj.name === name) console.log('found!');
return obj.name === name;
});
}
var result = findThing('fridge');
console.log(result);
You need to assign the result of filter to a object and you get the result as the [id]. You then need to get the object as this.props.things[id]
var data = {
things: {
1: {
name: 'fridge',
attributes: []
},
2: {
name: 'ashtray',
attributes: []
}
}
}
var name = 'fridge';
var newD = Object.keys(data.things).filter((id) => {
if (data.things[id].name === name) console.log('found!');
return (data.things[id].name === name);
});
console.log(data.things[newD]);
I have the following object structure:
var mapData =
{
Summary:
{
ReportName: 'Month End Report'
},
NortheastRegion:
{
Property1: 123,
RegionName: 'Northeast'
},
SoutheastRegion:
{
Property1: 456,
RegionName: 'Southeast'
},
}
I want to write a grep function that returns an array of region names. The following function is not returning any values:
var regions = $.grep(mapData, function(n,i)
{
return n.RegionName;
});
What am I missing here?
$.grep is for filtering arrays. Your structure isn't an array. $.grep is also just for filtering, but you're talking about both filtering (leaving out Summary) and mapping (getting just the region names).
Instead, you can use
Object.keys and push:
var regions = [];
Object.keys(mapData).forEach(function(key) {
var entry = mapData[key];
if (entry && entry.RegionName) {
regions.push(entry.RegionName);
}
});
Object.keys, filter, and map:
var regions = Object.keys(mapData)
.filter(function(key) {
return !!mapData[key].RegionName;
})
.map(function(key) {
return mapData[key].RegionName;
});
A for-in loop and push:
var regions = [];
for (var key in mapData) {
if (mapData.hasOwnProperty(key)) {
var entry = mapData[key];
if (entry && entry.RegionName) {
regions.push(entry.RegionName);
}
}
}
...probably others.
That's an object, not an array. According to the jQuery docs, your above example would work if mapData were an array.
You can use lodash's mapValues for this type of thing:
var regions = _.mapValues(mapData, function(o) {
return o.RegionName;
});
ES6:
const regions = _.mapValues(mapData, o => o.RegionName)
As stated in jQuery.grep() docs, you should pass an array as data to be searched, but mapData is an object. However, you can loop through the object keys with Object.keys(), but AFAIK you'll have to use function specific for your case, like:
var mapData =
{
Summary:
{
ReportName: 'Month End Report'
},
NortheastRegion:
{
Property1: 123,
RegionName: 'Northeast'
},
SoutheastRegion:
{
Property1: 456,
RegionName: 'Southeast'
},
};
var keys = Object.keys(mapData),
result = [];
console.log(keys);
keys.forEach(function(key) {
var region = mapData[key].RegionName;
if (region && result.indexOf(region) == -1) {
result.push(region);
}
});
console.log(result);
// Short version - based on #KooiInc answer
console.log(
Object.keys(mapData).map(m => mapData[m].RegionName).filter(m => m)
);
$.grep is used for arrays. mapData is an object. You could try using map/filter for the keys of mapData, something like:
var mapData =
{
Summary:
{
ReportName: 'Month End Report'
},
NortheastRegion:
{
Property1: 123,
RegionName: 'Northeast'
},
SoutheastRegion:
{
Property1: 456,
RegionName: 'Southeast'
},
};
var regionNames = Object.keys(mapData)
.map( function (key) { return mapData[key].RegionName; } )
.filter( function (name) { return name; } );
console.dir(regionNames);
// es2105
var regionNames2 = Object.keys(mapData)
.map( key => mapData[key].RegionName )
.filter( name => name );
console.dir(regionNames2);
Just turn $.grep to $.map and you would good to go.
var regions = $.map(mapData, function(n,i)
{
return n.RegionName;
});
I want to map a $scope.filters object to a var criteria on the condition if the original fields are null or not.
So lets say I have:
$scope.filters = {
name: 'myName'
lastName: null,
age: null,
}
I want my var criteria to be mapped to non null fields like this:
var criteria = {
name: 'myName';
}
So I have tried like this:
var criteria = {};
angular.forEach($scope.filters, function (value, key, obj) {
if (value != null) {
this.push(obj)
}
}, criteria);
But I guess I'm missing something.
criteria is an object, not an array, so you can't push it. Either make it an array if you want to store separate individual criteria, or replace the push with this[key] = value;
You should do it like this,
$scope.filters = {
name: 'myName'
lastName: null,
age: null,
}
var criteria = []; //array, you were using object
angular.forEach($scope.filters, function(value, key) {
if (value != null) {
this.push(key + ': ' + value);
}
}, criteria);
expect(criteria).toEqual(['name: myName']);
Hope it helps.