I have three arrays filled with objects in JavaScript that I've put into a multi-dimensional array in order to get all possible combinations of all three objects to create a set of 'rules'. Then later in the code I want to be able to pull out the properties of these objects, but when I attempt to all I seem to be able to get is the string "[object]" and not the actual object itself.
This is how the array is put together:
var allArrays = [settings.serviceLevels, settings.serviceDays, settings.services];
function allPossibleCases(arr) {
if (arr.length === 1) {
return arr[0];
} else {
var result = [];
var allCasesOfRest = allPossibleCases(arr.slice(1));
for (var i = 0; i < allCasesOfRest.length; i++) {
for (var j = 0; j < arr[0].length; j++) {
result.push(arr[0][j] + allCasesOfRest[i]);
}
}
return result;
}
}
var uncheckedRules = allPossibleCases(allArrays);
I need to be able to get the properties out of settings.serviceLevels, settings.serviceDays and settings.services - I can find questions that do this with regular arrays but not with objects - is this even possible or have I taken it to a level where I've lost the properties?
Not sure I understood what you wanted. But here's a recursive function that will store in an array the properties of objects contained in another array :
function listProps(object, result) {
// for each property of the object :
for (objectProperty in object) {
// if it's an object, recursive call :
if (typeof object[objectProperty] === 'object') {
listProps(object[objectProperty], result);
} else { // otherwise, push it into the result array :
result.push(object[objectProperty]);
}
}
}
var allArrays = [settings.serviceLevels, settings.serviceDays, settings.services];
var result = [];
listProps(allArrays, result);
The result array should list every properties from the three objects settings.serviceLevels, settings.serviceDays, settings.services and their children (if they contain any object themselves).
Related
I have a function which goes through every name in an object. This object has an array parameter materials which holds other objects. Those objects have the same array parameter which hold more objects and so on.
My code lines 3-5 just repeat while adding .material and another variable. Also the code will only go into an object's name four layers down or else I have to keep repeating more code.
How can I cut down on this mess?
var itemList = function(x) {
console.log(x.materials[0].name);
for (var i = 1; i < x.materials.length; i++) {
console.log(x.materials[i].name);
if (x.materials[i].build !== "BasicFactory" && i !== 0) {
for (var j = 1; j < x.materials[i].materials.length; j++) {
console.log(x.materials[i].materials[j].name);
if (x.materials[i].materials[j].build !== "BasicFactory" && j !== 0) {
for (var k = 1; k < x.materials[i].materials[j].materials.length; k++) {
console.log(x.materials[i].materials[j].materials[k].name);
if (x.materials[i].materials[j].materials[k].build !== "BasicFactory" && j !== 0) {
for (var l = 1; l < x.materials[i].materials[j].materials[k].materials.length; l++) {
console.log(x.materials[i].materials[j].materials[k].materials[l].name);
}
}
}
}
}
}
}
};
Use map to iterate and get the key/value pairs, and filter to remove other values:
function foo(value, index)
{
if (value.hasOwnProperty("name")) return value.name; else return null;
}
function bar(value)
{
if (value) return value;
}
var baz =
[
1,
2,
{name:[1,
2,
{name:[1,2,3]}
]
}
].map(foo).filter(bar);
console.log(baz);
Sometimes we might want to get the transformation or the mapping of the original array. JavaScript provides a HOF for that too: map. This function has a signature, as follows:
array.map(callback,[ thisObject])
This method also applies callback functions for each element of an array (again, only in the required context of this, and only for existing items). It does however also return the transformed (mapped) array as a result.
Instead of the basic mapped result, we may want to only get certain entries that satisfy a certain condition, for example ones that have an email address that starts with "d". We can create a filter for exactly this kind of purpose, which will exclude items that don't pass our conditions. The filter method can be used to do this quickly and easily.
The signature is quite similar to that of map:
array.filter(callback,[ thisObject])
The callback function of the filter should return the boolean value (either true or false). true means that the filter is passed, and false means that an item shouldn’t be included in the result set.
References
Array Extras in Detail
I have an array of objects which contain certain duplicate properties: Following is the array sample:
var jsonData = [{x:12, machine1: 7}, {x:15, machine2:7},{x:12, machine2: 8}];
So what i need is to merge the objects with same values of x like the following array:
var jsonData = [{x:12, machine1:7, machine2:8}, {x:15, machine2:7}]
I like the lodash library.
https://lodash.com/docs#groupBy
_.groupBy(jsonData, 'x') produces:
12: [ {x=12, machine1=7}, {x=12, machine2=8} ],
15: [ {x=15, machine2=7} ]
your desired result is achieved like this:
var jsonData = [{x:12, machine1: 7}, {x:15, machine2:7},{x:12, machine2: 8}];
var groupedByX = _.groupBy(jsonData, 'x');
var result = [];
_.forEach(groupedByX, function(value, key){
var obj = {};
for(var i=0; i<value.length; i++) {
_.defaults(obj, value[i]);
}
result.push(obj);
});
I'm not sure if you're looking for pure JavaScript, but if you are, here's one solution. It's a bit heavy on nesting, but it gets the job done.
// Loop through all objects in the array
for (var i = 0; i < jsonData.length; i++) {
// Loop through all of the objects beyond i
// Don't increment automatically; we will do this later
for (var j = i+1; j < jsonData.length; ) {
// Check if our x values are a match
if (jsonData[i].x == jsonData[j].x) {
// Loop through all of the keys in our matching object
for (var key in jsonData[j]) {
// Ensure the key actually belongs to the object
// This is to avoid any prototype inheritance problems
if (jsonData[j].hasOwnProperty(key)) {
// Copy over the values to the first object
// Note this will overwrite any values if the key already exists!
jsonData[i][key] = jsonData[j][key];
}
}
// After copying the matching object, delete it from the array
// By deleting this object, the "next" object in the array moves back one
// Therefore it will be what j is prior to being incremented
// This is why we don't automatically increment
jsonData.splice(j, 1);
} else {
// If there's no match, increment to the next object to check
j++;
}
}
}
Note there is no defensive code in this sample; you probably want to add a few checks to make sure the data you have is formatted correctly before passing it along.
Also keep in mind that you might have to decide how to handle instances where two keys overlap but do not match (e.g. two objects both having machine1, but one with the value of 5 and the other with the value of 9). As is, whatever object comes later in the array will take precedence.
const mergeUnique = (list, $M = new Map(), id) => {
list.map(e => $M.has(e[id]) ? $M.set(e[id], { ...e, ...$M.get(e[id]) }) : $M.set(e[id], e));
return Array.from($M.values());
};
id would be x in your case
i created a jsperf with email as identifier: https://jsperf.com/mergeobjectswithmap/
it's a lot faster :)
In my PHP app, for 2 different tools I get 2 json responses but I would like to use the same javascript to handle both.
All I need to do is to compare this structure
data = [
{'k1':'v1'},
{'k2':'v2'},
{'k3':'v3'}
]
with this one:
data = {'k11':'v11', 'k22':'v22', 'k33':'v33'}
this must work for any number of results (they are not suppose to match) and it also needs to work if 1 result is given in any of both cases.
I've already tried
- using data.length (which I thought in the second case would give me 1 array
- data instanceof Array, which is true for both cases (same thing if I do data instanceof Object)
What is the best approach to compare these in javascript?
EDITED: the keys and values of both json are not suppose to match, I only want to compare the structure, or detect one of them without having the other. (array of objects vs object with properties)
The first one is an array of objects; the second is an object with properties.
If you need to do this on the PHP side, just do json_decode($data, true) on the second data structure (the second argument casts it as an array) before output. You may have to play around with it a little to get the structures to line up but it should be pretty straight-forward.
Otherwise, in js -
var data = [
{'k':'v'},
{'k2':'v2'},
{'k3':'v3'}
],
data2 = {'k':'v', 'k2':'v2', 'k3':'v3'},
data3 = [];
for (var x in data2) {
data3.push({x:data2[x]});
}
To check if the received data is in the first format or the second one you may just check if the data is an array or not.
The correct way to do it is as follows:
if( Object.prototype.toString.call( data ) === '[object Array]' )
{
// an array: parse data according to first format
}
else
{
// not an array: parse data according to second format
}
There are several way you can do this. I would flatten the array so that it matches the format of the object:
function Flatten(source) {
if (!source || !source.length || source.length === 0) {
return source;
}
var output = {};
for (var i = 0; i < source.length; i++) {
for (prop in source[i]) {
if (source[i].hasOwnProperty(prop)) {
output[prop] = source[i][prop];
}
}
}
return output;
}
Here's a fiddle
I'm not actually sure what you mean by "compare", but if you want a purely JavaScript solution to finding out if the structures are equivalent, you could use this.
Simple Shallow Solution
function numOwnProperties (obj) {
var i = 0;
for (var key in obj) {
if (obj.hasOwnProperty(key)) { i++; }
}
return i;
}
function areEquivalent (obj, arr) {
var length = arr.length;
if (numOwnProperties(obj) !== length) { return false; }
for (var i = 0; i < length; i++) {
var item = arr[i];
for (var key in item) {
if (item.hasOwnProperty(key) && item[key] !== obj[key]) {
return false;
}
}
}
return true;
};
Usage
data1 = [
{'k':'v'},
{'k2':'v2'},
{'k3':'v3'}
];
data2 = {
'k':'v',
'k2':'v2',
'k3':'v3'
};
console.log(areEquivalent(data2, data1)); // true
I have a class like below;
function Request()
{
this.CompanyId;
this.Password;
this.SessionId;
this.UserId;
this.UserName;
}
I create an object and want to get byte array of object;
var request = new Request();
request.UserName = GlobalProcess.SessionInfo.Server.UserName;
request.Password = GlobalProcess.SessionInfo.Server.Password;
request.CompanyId = GlobalProcess.SessionInfo.SelectedDatabase.CompanyId.toString();
request.UserId = GlobalProcess.SessionInfo.UserId.toString();
request.SessionId = GlobalProcess.SessionInfo.SessionId.toString();
var requestbinary = GetByte(request);
console.log(requestbinary);
My GetByte function is;
function GetByteArrayFromStringArray(parameter)
{
var mainbytesArray = [];
for (var i = 0; i < parameter.length; i++)
mainbytesArray.push(parameter.charCodeAt(i));
return mainbytesArray;
}
In console, I get empty array. What am I doing wrong?
Try this
function GetByteArrayFromStringArray(parameter) {
for (var key in parameter) { // loop through properties
var mainbytesArray = [];
for (var i = 0; i < parameter[key].length; i++)
mainbytesArray.push(parameter[key].charCodeAt(i));
}
return mainbytesArray;
}
It loops through the properties and gets you the array of theese
You're passing an object to a function that expects a string (I think). Your object has no "length" property, so the loop does nothing at all.
You could have the function iterate through the object's properties, I suppose, and accumulate an array from the values of each one. That would not be terribly useful, I don't think, as in JavaScript you're not guaranteed that you'll iterate through an object's properties in any particular order.
Consider this Array
var LIST =[];
LIST['C']=[];
LIST['B']=[];
LIST['C']['cc']=[];
LIST['B']['bb']=[];
LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');
I can loop through this array like
for(var i in LIST){
console.log(i)//C,B
var level1=LIST[i];
for(var j in level1){
console.log(j)//cc,bb
// etc...
}
}
Fine.. I have few basic questions.
1.How to sort the array in each level?
One level can be sort by .sort(fn) method . How can i pass to inner levels?
2.Why the indexOf method does not works to find the elements in first two levels?
If it's because of the a non string parameter .. how can i search an array items in array if the item is not string?
3.How for(var i in LIST) works ?
I just need a basic understanding of indexing and looping through array ..
Thanks ..
LIST is NOT a three dimensional array in Javascript, it is just an array.
//declare an array which names LIST.
var LIST = [];
//set a property named 'C' of the LIST to be an array.
LIST['C']=[];
//set a property named 'B' of the LIST to be an array.
LIST['B']=[];
//set a property named 'cc' of the 'LIST.C'(which is an array object)
LIST['C']['cc']=[];
//set a property named 'bb' of the 'LIST.B'(which is an array object)
LIST['B']['bb']=[];
The fact is you only need to let the last level to be an array, see my example code below.
function iterateOrderd(obj) {
if (obj instanceof Array) {
obj.sort();
for (var j = 0, l=obj.length; j < l; j++) {
console.log(obj[j]);
}
} else {
var sortable = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
sortable.push(i);
}
}
sortable.sort();
for (var j = 0, l=sortable.length; j < l; j++) {
console.log(sortable[j]);
iterateOrderd(obj[sortable[j]]);
}
}
}
var LIST = {};
LIST['C'] = {};
LIST['B'] = {};
LIST['C']['cc']=[];
LIST['B']['bb']=[];
LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');
iterateOrderd(LIST);
You need to know that Array inherits from Object.
In JavaScript, any Object instance is an associative array(!), so acts like an Array in PHP. For example:
var o = {}; // or new Object();
o['foo'] = 'bar';
o[0] = 'baz';
for (i in o) { console.log(i, o[i]); }
Sorting an Object does not make much sense. indexOf would kinda work in theory, but is not implemented.
Arrays are ordered lists. Array instances have push(), length, indexOf(), sort() etc., but those only work for numerical indexes. But again, Array inherits from Object, so any array can also contain non-numerical index entries:
var a = []; // or new Array();
a[0] = 'foo'; // a.length is now 1
a.push('baz'); // a[1] === 'baz'
a.qux = 1; // will not affect a.length
a.sort(); // will not affect a.qux
for (i in a) { console.log(i, a[i]); }
I recommend playing around with arrays and objects, and you'll soon get the point.
What is your sorting criteria ? I mean how will you say array firstArray comes before secondArray?
regarding the for (counter in myArray), counter will take values of an array element in every iteration.
for (counter in [0,1,5]), counter will have values 0, 1 and 5 in the 3 iterations.
In your case, i will have values LIST['B'] and LIST['C'] in the two iterations and j will have values LIST['B']['bb'], LIST['B']['cc'], LIST['C']['bb'] and LIST['C']['cc'].
Both i and j will be arrays.