Pure JavaScript JSON compare array - javascript

I have to compare some JSON data and return index of position. I use pure JavaScript.
I have data below:
NAMES
[
{
"someName" : Name1,
"secondData" : "aaaa1",
"thirdData" : bbbb1
},
{
"someName" : Name2,
"secondData" : "aaaa2",
"thirdData" : bbbb2
},
{
"someName" : Name3,
"secondData" : "aaaa3",
"thirdData" : bbbb3
},
{
"someName" : Name4,
"secondData" : "aaaa4",
"thirdData" : bbbb4
}
]
and JSON SEATS
[
{
"seats" : 0,
"someName" : "Name4",
omeData" : null,
"someData" : null
},
{
"seats" : 1,
"someName" : "Name3",
"someData" : null,
"someData" : null
},
{
"seats" : 2,
"someName" : "Name1",
"someData" : null,
"someData" : null
},
{
"seats" : 3,
"someName" : "Name2",
"someData" : null,
"someData" : null
}
]
All what I want to do is compare this JSON like this:
Take someName from NAMES.someName and search the same name in SEATS.someName if is the same create new array RESULTS. Index in RESULTS array shuldbe SEATS.seats and data from NAMES.
Example below
RESULTS[0] = NAMES[{"someName" : Name4,"secondData" : "aaaa4","thirdData" : bbbb4}]
RESULTS[1] = NAMES[{"someName" : Name3,"secondData" : "aaaa3","thirdData" : bbbb3}]
RESULTS[2] = NAMES[{"someName" : Name1,"secondData" : "aaaa1","thirdData" : bbbb1}]
RESULTS[3] = NAMES[{"someName" : Name2,"secondData" : "aaaa2","thirdData" : bbbb2}]
I start do this like this but I stack
for(i=0;i<=459; i++) {
if(mergeData[i][2] == jsonData[4].rows[i].find(compareElement)) {
}
}
function compareElement(element) {
return element.someName == i;
}
Problem is if I use find like this I have some error and program stop I can't find way how to solve this.

var result = [];
NAMES.forEach(function(name) {
var found = SEATS.find(function(seat) {
return name.someName === seat.someName
});
if (found) {
result[found.seats] = name;
}
});

This solution works for both Objects as well as Arrays.
function compare(obj: any, obj2: any): boolean {
if (typeof obj !== typeof obj2) {
return false;
}
if (typeof obj !== "object" && typeof obj2 !== "object") {
return obj === obj2;
} else {
for (let key in obj) {
if (key) {
return compare(obj[key], obj2[key]);
}
}
return false;
}
}

Related

How do I iterate a nested object and return all values where property is "something"

I would like to know the easiest way to return an array of values from a nested object where there is some specific property. The given object has many properties with the same name all over the place and keep in mind my object is very large and there is no pattern to where the property might be repeated..
for example:
var object = {
car : { price : "100" },
lot : { bike : { one : { price : "4" }, two { price : "16" } } }
bread : { crust : { price : "2" } }
}
findAllValues( object, "price" ) // returns array ["100", "4", "16", "2"]
I've tried to use some libraries like lodash but I can't seem to get it right. Any response would be greatly appreciated!
You can use a recursive function to recurse through your object and return an array.
You can use the following function; all you have to do is pass an object and the property to match.
const object = {
car : { price : "100" },
lot : { bike : { one : { price : "4" }, two : { price : "16" } } },
bread : { crust : { price : "2" } }
}
const findAllValues = (obj, match) => {
const res = []
const recurse = (obj) => {
for (const key in obj) {
const value = obj[key]
if (value !== undefined) {
if (value && typeof value === 'object') {
recurse(value, key)
} else {
if (key === match) {
res.push(value)
}
}
}
}
}
recurse(obj)
return res
}
console.log(findAllValues(object, "price"))
In this solution, we use a deepSearch function that recursively dives down any arbitrary object and, if there's a key that matches our provided key, we tack it on to the provided array.
const object = {
car : { price : "100" },
lot : { bike : { one : { price : "4" }, two : { price : "16" } } },
bread : { crust : { price : "2" } }
}
console.log(findAllValues(object, "price"));
function findAllValues(object, key) {
const arr = [];
deepSearch(object, key, arr);
return arr;
}
function deepSearch(obj, key, array) {
Object.entries(obj).forEach(([k, value]) => {
if (k === key) {
array.push(value);
} else if (typeof value === "object") {
deepSearch(value, key, array);
}
})
}

Lodash. Smart merge two objects by keys

I have the two objects. A and B.
A
{
"beta" : {
"value": null,
"error" : null
},
"hamma" : {
"value": null,
"error" : null
},
"zerta" : {
"value": null,
"error" : null
},
"mozes" : 5
}
B
{
"beta" : 5,
"hamma" : 2
}
How do I can loop through the A keys, compare it with the B object and update the values of the existing keys in the A object via Lodash? Maybe there is exists some nice way? I tried to use "assing, assignWith" but looks like I haven't understood how it works.
The result should looks like that:
{
"beta" : {
"value": 5,
"error" : null
},
"hamma" : {
"value": 2,
"error" : null
},
"zerta" : {
"value": null,
"error" : null
},
"mozes" : 5
}
Thanks for any information.
I have resolved this solution via native js by that way but I want to know how can I do it via Lodash.
export function mapServerModelToStateValues(state, serverModel) {
let updatedState = {};
const serverModelKeyList = Object.keys(serverModel);
Object.keys(state).forEach(stateKey => {
serverModelKeyList.forEach(modelKey => {
if ( modelKey === stateKey ) {
updatedState[ stateKey ] = {
...state[ stateKey ],
value : serverModel[ modelKey ]
}
}
});
});
console.log(updatedState);
}
You can use _.mergeWith lodash method and pass custom function.
var a = {"beta":{"value":null,"error":null},"hamma":{"value":null,"error":null},"zerta":{"value":null,"error":null},"mozes":5}
var b = {"beta":5,"hamma":2, "mozes": 123}
_.mergeWith(a, b, function(aValue, bValue) {
_.isPlainObject(aValue) ? aValue.value = bValue : aValue = bValue
return aValue
})
console.log(a)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Here's a solution that iterates over the B array and uses set to update the value:
_.each(updates, (value, key) => _.set(data, `${key}.value`, value))
This will do the trick, notice that the result will have all the keys in the same value, error format
const A = {
"beta": {
"value": null,
"error": null
},
"hamma": {
"value": null,
"error": null
},
"zerta": {
"value": null,
"error": null
},
"mozes": 5
}
const B = {
"beta": 5,
"hamma": 2
}
const C = _.mapValues(A, (value, key) => {
return {
...value,
value: B[key] || value.value
}
});
console.log(C)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Map through the _keys from your second object to update the first object accordingly:
// obj a
const a = {
beta: {
value: null,
error: null
},
hamma: {
value: null,
error: null
},
zerta: {
value: null,
error: null
},
mozes: 5
};
// obj b
const b = {
beta: 5,
hamma: 2
};
// map through the 'obj b' keys and update 'obj a' accordingly
_.keys(b).map(key => {
a[key].value = b[key];
});
// log obj a to the console
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Update: I would use the solution from #GruffBunny - the idea is similar to this one, but done much more elegantly using lodash.
You can use lodash#merge and lodash#mapValues to achieve this.
var result = _.merge({}, a, _.mapValues(b, value => ({ value })));
var a = {
"beta" : {
"value": null,
"error" : null
},
"hamma" : {
"value": null,
"error" : null
},
"zerta" : {
"value": null,
"error" : null
},
"mozes" : 5
};
var b = {
"beta" : 5,
"hamma" : 2
};
var result = _.merge({}, a, _.mapValues(b, value => ({ value })));
console.log(result);
.as-console-wrapper { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Javascript multiple condition array filter

I need help putting together an array search that is based on multiple conditions. Furthermore, all the conditions are conditional, meaning I may or may not need to filter on those conditions. What I have:
Array of objects to filter:
var data = [{
"_id" : ObjectId("583f6e6d14c8042dd7c979e6"),
"transid" : 1,
"acct" : "acct1",
"transdate" : ISODate("2012-01-31T05:00:00.000Z"),
"category" : "category1",
"amount" : 103
},
{
"_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
"transid" : 2,
"acct" : "acct2",
"transdate" : ISODate("2012-01-31T05:00:00.000Z"),
"category" : "category2",
"amount" : 103
},
{
"_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
"transid" : 3,
"acct" : "acct2",
"transdate" : ISODate("2016-07-31T05:00:00.000Z"),
"category" : "category1",
"amount" : 103
},
{
"_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
"transid" : 4,
"acct" : "acct2",
"transdate" : ISODate("2012-01-31T05:00:00.000Z"),
"category" : "category2",
"amount" : 103
},
{
"_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
"transid" : 5,
"acct" : "acct2",
"transdate" : ISODate("2012-01-31T05:00:00.000Z"),
"category" : "category3",
"amount" : 103
},
{
"_id" : ObjectId("583f6e6d14c8042dd7c152g2"),
"transid" : 6,
"acct" : "acct3",
"transdate" : ISODate("2016-10-31T05:00:00.000Z"),
"category" : "category3",
"amount" : 103
}]
I am filtering the above array of objects based on another array of mixed elements. The elements represent the following search fields:
"searchstring": to search on all fields in the data array for any
matched text sequence
object with key values reprsenting account type and a true or false
for value indicating if it should be used to filter
startdate to filter transdate on
enddate to filter transdate
category name to filter category on
The array that has the search conditions looks like this (but if some of the fields are not necessary they will be set to undefined or just an empty string or array):
var filtercondition = {
"p",
{acct1:true,acct2:false,acct3:true...}
"2016-06-01",
"2016-11-30",
"category3"
}
What is the best way to accomplish this? What I've devised is a separate search for each element in the filter array, but this seems non optimal and very tedious. I'm open to a redesign of my setup...
// You wrote that it's an array, so changed the braces
var filtercondition = ["p",
{acct1:true,acct2:false,acct3:true...}
"2016-06-01",
"2016-11-30",
"category3"
];
var filtered = data.filter(o => {
if(filtercondition[0] && !o.category.includes(filtercondition[o])) { // checking just the category, but you can check if any of more fields contains the conditions
return false;
}
if(filtercondition[1]) {
for(var key in filtercondition[1]) {
if(filtercondition[1][key] === true && o.acct != key) {
return false;
}
}
}
if(filtercondition[2] && o.transdate < filtercondition[2]) {
return false;
}
if(filtercondition[3] && o.transdate > filtercondition[3]) {
return false;
}
if(filtercondition[4] && o.category !== filtercondition[4]) {
return false;
}
return true;
});
Two notes:
- changed the braces of filtercondition so that it is an array, however I would suggest to use an object instead.
- this {acct1:true,acct2:false,acct3:true...} sample doesn't make sense for me, since it suggests that the acct field should be acct1 and acct3 at the same time.
Create an array of functions, each function representing a condition.
Here's some sample code which demonstrates the approach...
var conditions = [];
// Dynamically build the list of conditions
if(startDateFilter) {
conditions.push(function(item) {
return item.transdate >= startDateFilter.startDate;
});
};
if(categoryFilter) {
conditions.push(function(item) {
return item.cateogry === categoryFilter.category;
});
};
// etc etc
Once you have an array of conditions, you can use Array.prototype.every to run each condition on an item.
var itemsMatchingCondition = data.filter(function(d) {
return conditions.every(function(c) {
return c(d);
});
});
Or, using the more compact arrow functions:
const itemsMatchingCondition = data.filter(d => conditions.every(c => c(d));
First, you'll want to use brackets for your array not curly braces:
var filtercondition = [
"p",
{acct1:true,acct2:false,acct3:true...},
"2016-06-01",
"2016-11-30",
"category3"
];
Then again, I don't think that an array is the best data type for that. Try an object like this:
var filtercondition = {
query: "p",
accounts: {acct1:true,acct2:false,acct3:true...},
date1: "2016-06-01",
date2: "2016-11-30",
category: "category3"
};
Then, try using Array.prototype.filter:
var filtered = data.filter(function(obj) {
for (var key in filtercondition) {
// if condition not met return false
}
return true;
});
I'd go with a bunch of small granular functions and compose them.
//only some utilities, from the top of my mind
var identity = v => v;
//string-related
var string = v => v == null? "": String(v);
var startsWith = needle => haystack => string(haystack).startsWith(needle);
var endsWith = needle => haystack => string(haystack).endsWith(needle);
var contains = needle => haystack => string(haystack).contains(needle);
//do sth with an object
var prop = key => obj => obj != null && prop in obj? obj[prop]: undefined;
var someProp = fn => obj => obj != null && Object.keys(obj).some(k => fn(k) );
var someValue = fn => obj => obj != null && Object.keys(obj).some(k => fn(obj[k]) );
//logic
var eq = b => a => a === b;
var not = fn => function(){ return !fn.apply(this, arguments) };
var and = (...funcs) => funcs.reduce((a, b) => function(){
return a.apply(this, arguments) && b.apply(this, arguments);
});
var or = (...funcs) => funcs.reduce((a, b) => function(){
return a.apply(this, arguments) || b.apply(this, arguments);
});
//composition
var compose = (...funcs) => funcs.reduce((a, b) => v => return a(b(v)));
var chain = (...funcs) => funcs.reduceRight((a, b) => v => return a(b(v)));
//and whatever else you want/need
//but stay granular, don't put too much logic into a single function
and an example composition:
var filterFn = and(
//some value contains "p"
someValue(contains("p")),
//and
chain(
//property "foo"
prop("foo"),
or(
//either contains "asdf"
contains("asdf"),
//or startsWith "123"
startsWith("123")
)
),
)
since I don't know how you build your filterconditions, I cannot tell you exactly how to parse them into such a composition, but you could compose them like this:
//start with something basic, so we don't ever have to check wether filterFn is null
var filterFn = identity;
//and extend/compose it depending on some conditions
if(/*hasQuery*/){
filterFn = and(
// previous filterFn(obj) && some value on obj contains `query`
filterFn,
someValue(contains(query)))
)
}
if(/*condition*/){
//extend filterFn
filterFn = or(
// (obj.foo === null) || previous filterFn(obj)
chain(prop("foo"), eq(null)),
filterFn
);
}
and so on
First, some points:
Your data object is invalid if you're going to use it in the browser. Probably the data comes from MongoDB, right? Your backend (data source) should have a method to encode it properly and remove ObjectID and ISODate references.
Your filtercondition is not a valid JavaScript object/JSON. Check my example.
So, you can filter your data array with Array#filter method.
Something like that:
let data = [{
"_id" : "583f6e6d14c8042dd7c979e6",
"transid" : 1,
"acct" : "acct1",
"transdate" : "2012-01-31T05:00:00.000Z",
"category" : "category1",
"amount" : 103
},
{
"_id" : "583f6e6d14c8042dd7c2132t6",
"transid" : 2,
"acct" : "acct2",
"transdate" : "2012-01-31T05:00:00.000Z",
"category" : "category2",
"amount" : 103
},
{
"_id" : "583f6e6d14c8042dd7c2132t6",
"transid" : 5,
"acct" : "acct2",
"transdate" : "2012-01-31T05:00:00.000Z",
"category" : "category3",
"amount" : 103
}];
let filterToApply = {
acct: {
acct1: true,
acct2: false,
acct3: true
},
initialDate: "2016-06-01",
finalDate: "2016-11-30",
category: "category3"
}
let filterData = (array, filter) => {
return array.filter( (item) => {
/* here, you iterate each item and compare with your filter,
if the item pass, you must return true. Otherwise, false */
/* e.g.: category check (if present only) */
if (filter.category && filter.category !== item.category)
return false;
}
/* add other criterias check... */
return true;
});
}
let dataFiltered = filterData(data, filterToApply);
console.log(dataFiltered);
If you want to filter an array with multiple conditions and the conditions may be optional, then use the following method.
const data = [
{ name: 'John', age: 25, city: 'New York' },
{ name: 'John', age: 25, city: 'New' },
{ name: 'Jane', age: 32, city: 'Los Angeles' },
{ name: 'Bob', age: 45, city: 'New York' },
{ name: 'Alice', age: 38, city: 'Los Angeles' }
];
const filteredData = (n, c, a) => data.filter(item => {
if (n || c || a) {
return (n ? item.name === n : true) && (c ? item.city === c : true) && (a ? item.age === a : true); // keep adding conditons as much as u want
}
});
console.log(filteredData('John', null, 25));
console.log(filteredData(null, 'Los Angeles', 38));
console.log(filteredData(null, 'Los Angeles', null));
You can chain as many as conditions

Check if there are null values in an array of objects

I have this array of objects.
[Object, Object, Object]
0:Object
name: "Rick"
Contact: "Yes"
Date:'null'
Location:'null'
1:Object
name:"Anjie"
Contact:"No"
Date:'13/6/2016'
Location:'LA'
2:Object
name:"dillan"
Contact:"Maybe"
Date:'17/6/2016'
Location:'NY'
As you can see, there are null values for Object[0] for Date and Location. I want to check if there is a null value present in the entire array of Objects.
If there is a null value present for 'Date' and 'Location', i should be able to display 'Null present' at the console. If no null values are present, it should display 'Data right' at the console.
can someone please let me know how to achieve this.
var wasNull = false;
for(var i in objectsArray) {
if(objectsArray[i].Date == null || objectsArray[i].Location == null) wasNull = true;
}
if(wasNull) console.log('Was null');
else console.log('Data right');
Do it using Object.keys() and Array#some methods
var data = [{
name: "Rick",
Contact: "Yes",
Date: null,
Location: null
}, {
name: "Anjie",
Contact: "No",
Date: '13/6/2016',
Location: 'LA'
}, {
name: "dillan",
Contact: "Maybe",
Date: '17/6/2016',
Location: 'NY'
}];
// iterate over array elements
data.forEach(function(v, i) {
if (
// get all properties and check any of it's value is null
Object.keys(v).some(function(k) {
return v[k] == null;
})
)
console.log('null value present', i);
else
console.log('data right', i);
});
Use some() and check if there is a null.
var arr = [
{ a : "a", b : "b", c : "c" },
{ a : "a", b : "b", c : "c" },
{ a : "a", b : "b", c : null }
];
function hasNull(element, index, array) {
return element.a===null || element.b===null || element.c===null;
}
console.log( arr.some(hasNull) );
If you do not want to hardCode the if, than you need to add another loop and loop over the keys.
var arr = [
{ a : "a1", b : "b1", c : "c1" },
{ a : "a2", b : "b2", c : "c2" },
{ a : "a3", b : "b3", c : null }
];
function hasNull(element, index, array) {
return Object.keys(element).some(
function (key) {
return element[key]===null;
}
);
}
console.log( arr.some(hasNull) );
or JSON with a reg exp (
var arr = [
{ a : "a1", b : "b1", c : "c1" },
{ a : "a2", b : "b2", c : "c2" },
{ a : "a3", b : "b3", c : null }
];
var hasMatch = JSON.stringify(arr).match(/:null[\},]/)!==null;
console.log(hasMatch);
A solution using underscore:
var nullPresent = _.some(data, item => _.some(_.pick(item, 'Date', 'Location'), _.isNull));

Move an object (element) one step up with Javascript

I have several objects like this:
I want to move type and value one step up so they will be next to field, and then delete data.
It looks like this when departments is converted to JSON:
[
{"field" : "DEPARTMAN_NO",
"data" : { "type":"numeric" , "comparison":"eq" , "value":11 }
},
{"field" : "DEPARTMAN_ADI",
"data" : { "type":"string" , "value":"bir" }
}
]
I have tried:
departments = grid.filters.getFilterData();
i = {};
for(var i in department) {
department = i.data;
delete.department.data;
};
but it dosen't work.
1) First, loop departments, each item we call it department;
2) You want to move department.data's properties to department, From another angle, you can move department's properties to department.data and return department.data, code like:
var departments = [{
"field": "DEPARTMAN_NO",
"data": {
"type": "numeric",
"comparison": "eq",
"value": 11
}
}, {
"field": "DEPARTMAN_ADI",
"data": {
"type": "string",
"value": "bir"
}
}],
department;
for (var i = 0, len = departments.length; i < len; i++) {
department = departments[i]; // department
for (var key in department) {
if (key !== 'data' && department.data) {
department.data[key] = department[key];
}
}
departments[i] = department.data || department; // if no department.data, no change
}
console.log(departments);
result:
view the full demo http://jsfiddle.net/KVYE5/
I wrote a little npm package that does what you're asking for: moving a property up a level in an object.
You can get it here: https://www.npmjs.com/package/move-property-up-a-level
Usage
var movePropertyUpALevel = require('movePropertyUpALevel');
var fakeObj = {
poodle: {
first: {
hey: 'you'
},
second: 'meAgain'
}
};
movePropertyUpALevel(fakeObj, 'poodle');
console.log(fakeObj.first.hey);
//'you'
console.log(fakeObj.poodle);
//undefined
obj =
[
{"field" : "DEPARTMAN_NO",
"data" : { "type":"numeric" , "comparison":"eq" , "value":11 }
},
{"field" : "DEPARTMAN_ADI",
"data" : { "type":"string" , "value":"bir" }
}
];
for ( var item in obj ) {
if ( obj[item].field && obj[item].data ) { //check the 'field' and 'data' exist
obj[item].field = {
dept : obj[item].field , //department name is put into a property
type : obj[item].data.type, //so is data.type and data.value..
value: obj[item].data.value //..all are now contained in 'field'
};
delete obj[item].data; //remove the 'data' object
}
}
console.log(obj);
department.type = department.data.type;
department.value = department.data.value;
delete department['data'];

Categories

Resources