I am a javascript beginner. I need to merge two arrays which contains objects which in turn contain arrays.
I have two arrays
arr1[
{
description : "this is a object",
array : [a.x,"b"]
}
]
arr2[
{
array : [a.z,"b","c","d"]
}
]
I have used the following code to perform the merge
function arrayUnique(array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
}
function combine(obj1,obj2) {
var res = {};
for (var k1 in obj1) {
if (!obj1.hasOwnProperty(k1)) continue;
if (obj2.hasOwnProperty(k1)) { // collision
if (typeof(obj1[k1]) !== typeof(obj2[k1])) throw "type mismatch under key \""+k1+"\".";
if (Array.isArray(obj1[k1])) {
res[k1] = obj1[k1].concat(obj2[k1]);
} else if (typeof(obj1[k1]) === 'string' || obj1[k1] instanceof String) {
res[k1] = arrayUnique(obj1[k1].concat(obj2[k1]));
} else if (typeof(obj1[k1]) === 'object') {
res[k1] = combine(obj1[k1],obj2[k1]);
} else {
throw "unsupported collision type "+typeof(obj1[k1])+" under key \""+k1+"\".";
}
} else {
res[k1] = obj1[k1];
}
}
for (var k2 in obj2) {
if (!obj2.hasOwnProperty(k2)) continue;
if (obj1.hasOwnProperty(k2)) continue; // already handled it above
res[k2] = obj2[k2];
}
return res;
}
var res = combine(arr1,arr2);
This is the result i expect
res = { description : "this is a object", array : [a.x,a.z,"b","c","d"] }
But unfortunately this is the result i get
res = { description : "this is a object", array : [a.x,"b","c","d"]}
a.z is ommited.
When both objects have the same array field you concatenate them (concat append the two arrays one after another), here:
if (Array.isArray(obj1[k1])) {
res[k1] = obj1[k1].concat(obj2[k1]);
If instead of ["a","a","b","b","c","d"] you want to get ["a","b","c","d"] you need to perform the array merge manually.
Check this answer for multiple ways to merge arrays without duplicates.
Related
I need to convert a nested array into 2D in javascript, somewhat similar to the question answered for python at link
How to convert 2d nested array into 2d array single?
For example, the array
[[[[[[[[
[16,12],[16,13],[16,14]]
],
[[[[[[
[46,42],[46,43]
]]]]],[
[62,58],[62,59],[62,60]
]]]]]],
[103,102]],[[118,114],[118,115],[118,116]]
]
needs to be converted to
[[16,12],[16,13],[16,14],[46,42],[46,43],[62,58],[62,59],[62,60],[103,102],[118,114],[118,115],[118,116]]
Please help, thanks in advance
This is what I tried, finally works after many trials :
function removeNestArray2D(object) {
var result = [];
if (Array.isArray(object)) { // check if object is valid array
for(var i=0; i<object.length; i++) {
if(!Array.isArray(object[i])) { // check is each of array element is a valid array
return object;
}
else {
var tmp = removeNestArray2D(object[i]);
if(tmp.length == 1) {
result = tmp[0];
}
else if (tmp.length == 2 && Number.isInteger(tmp[0]) && Number.isInteger(tmp[1])) {
result.push(tmp);
}
else {
for (var j=0; j<tmp.length; j++) {
result.push(tmp[j]);
}
}
}
}
}
return result;
}
Recursive approach will help here. Check each array item if there are size 2 and both are number values then push to result array otherwise continue iteration recursively.
const arr = [[[[[[[[
[16,12],[16,13],[16,14]]
],
[[[[[[
[46,42],[46,43]
]]]]],[
[62,58],[62,59],[62,60]
]]]]]],
[103,102]],[[118,114],[118,115],[118,116]]
];
const get2dArray = arr => {
const res = [];
const pushRecursive = arr => {
if (arr.length == 2 && arr.every(x => Number.isInteger(x))) {
res.push(arr);
} else {
arr.forEach(pushRecursive);
}
};
pushRecursive(arr);
return res;
};
console.log(get2dArray(arr));
function removeNestArray2D(object) {
var result = [];
if (Array.isArray(object)) { // check if object is valid array
for(var i=0; i<object.length; i++) {
if(!Array.isArray(object[i])) { // check is each of array element is a valid array
return object;
}
else {
var tmp = removeNestArray2D(object[i]);
if(tmp.length == 1) {
result = tmp[0];
}
else if (tmp.length == 2 && Number.isInteger(tmp[0]) && Number.isInteger(tmp[1])) {
result.push(tmp);
}
else {
for (var j=0; j<tmp.length; j++) {
result.push(tmp[j]);
}
}
}
}
}
return result;
}
So my array looks like this:
let array = [
{"object1":1},
{"object2":2},
{"object3":3}
];
What I want to do is to check, for example, whether or not "object1" exists. The way I would prefer is pure Javascript.
I am doing this for large chunks of data and so my code needs to be something like this:
if ("opensprint1" in array){
console.log("yes, this is in the array");
} else {
console.log("no, this is not in the array");
};
NOTE: I have tried to use the (in) function in JS and the (hasOwnProperty) and neither has worked.
Any ideas?
if ("opensprint1" in array){
That check for the array keys, so it would work with:
if ("0" in array){
But actually you want to check if some of the array elements got that key:
if(array.some( el => "opensprint1" in el))
You're trying to filter an array of objects. You can pass a custom function into Array.prototype.filter, defining a custom search function. It looks like you want to search based on the existence of keys. If anything is returned, that key exists in the object array.
let array = [{
"object1": 1
},
{
"object2": 2
},
{
"object3": 3
}
];
const filterByKey = (arr, keyName) =>
array.filter(obj => Object.keys(obj).includes(keyName)).length > 0;
console.log(filterByKey(array, 'object1'));
console.log(filterByKey(array, 'object5'));
That is roughly equivalent to:
let array = [{
"object1": 1
},
{
"object2": 2
},
{
"object3": 3
}
];
const filterByKey = (arr, keyName) => {
// iterate each item in the array
for (let i = 0; i < arr.length; i++) {
const objectKeys = Object.keys(arr[i]);
// take the keys of the object
for (let j = 0; j < objectKeys.length; j++) {
// see if any key matches our expected
if(objectKeys[i] === keyName)
return true
}
}
// none did
return false;
}
console.log(filterByKey(array, 'object1'));
console.log(filterByKey(array, 'object5'));
This might help you
let array = [
{"object1":1},
{"object2":2},
{"object3":3}
];
let targetkey = "opensprint1";
let exists = -1;
for(let i = 0; i < array.length; i++) {
let objKeys = Object.keys(array[i]);
exists = objKeys.indexOf(targetkey);
if (exists >= 0) {
break;
}
}
if (exists >= 0) {
console.log("yes, this is in the array");
} else {
console.log("no, this is not in the array");
}
let array = [
{ "object1": 1 },
{ "object2": 2 },
{ "object3": 3 }
];
let checkKey = (key) => {
var found = false;
array.forEach((obj) => {
if (!(obj[key] === undefined)) {
found = true;
array.length = 0;
}
});
return found;
}
console.log(checkKey("object2"));
In this case, I think one of the most efficient way is to do a for and break like:
let array = [
{"object1":1},
{"object2":2},
{"object3":3}
];
exist = false;
for(let i = 0; i<array.length; i++){
if("object1" in array[i]){
exist = true;//<-- We just know the answer we want
break;//<-- then stop the loop
}
}
console.log(exist);
When iteration finds a true case, stops the iteration. We can't perform a break in .map, .filter etc. So the number of iterations are the less possible. I think this is also the case of .some()
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
How do i get object property using array of string (name of properties)? (the last element in array is the inner property of object)
See the code below:
Handy way:
let myObject = {
"property": {
"subproperty": {
"targetproperty": "Hi, We done it!"
}
}
};
let myString = "property:subproperty:targetproperty";
let parts = myString.split( ":" );
console.log( myObject[ parts[ 0 ] ][ parts[ 1 ] ][ parts[ 2 ] ] ); // Output: "Hi, We done it!"
Eval way:
let myObject = {
"property": {
"subproperty": {
"targetproperty": "Hi, We done it!"
}
}
};
let myString = "property:subproperty:targetproperty";
let parts = myString.split( ":" );
let code = "myObject";
for ( let i = 0; i < parts.length; i++ ) {
code += "['" + parts[ i ] + "']";
}
code += ";";
console.log( code );
console.log( eval( code ) ); // Output: "Hi, We done it!"
Eval is evil. so i need a cleaner way to do it.
How do i do it without eval and handy job?
You can use .reduce():
let myObject = {
"property": {
"subproperty": {
"targetproperty": "Hi, We done it!"
}
}
};
let myString = "property:subproperty:targetproperty";
let value = myString.split(":").reduce(function(obj, prop) {
return obj && obj[prop];
}, myObject);
console.log(value);
For loop:
function getByValue(arr, value) {
for (var i=0, iLen=arr.length; i<iLen; i++) {
if (arr[i].b == value) return arr[i];
}
}
.filter
function getByValue2(arr, value) {
var result = arr.filter(function(o){return o.b == value;} );
return result? result[0] : null; // or undefined
}
.forEach
function getByValue3(arr, value) {
var result = [];
arr.forEach(function(o){if (o.b == value) result.push(o);} );
return result? result[0] : null; // or undefined
}
If, on the other hand you really did mean for..in and want to find an object with any property with a value of 6, then you must use for..in unless you pass the names to check. e.g.
function getByValue4(arr, value) {
var o;
for (var i=0, iLen=arr.length; i<iLen; i++) {
o = arr[i];
for (var p in o) {
if (o.hasOwnProperty(p) && o[p] == value) {
return o;
}
}
}
}
You can use a reduce solution:
var obj = {prop1: {prop2: {prop3: 'xpto'}}};
var props = ['prop1','prop2','prop3'];
var result = props.reduce((acc,val)=>acc[val],obj);
console.log(result);
The recursive way ;)
Create a function that takes the current property, allparts and index.
Start with zero, return call to next index, try read and return with next call and increment index untill there are no more props to read/extract then return the value you got as the current property.
Let me know if you need working code
Here is a recursive approach, it will return undefined if the property isn't found:
const getPath = (o, keyPath, delimiter = '.') => {
if (Array.isArray(keyPath)) {
keyPath = keyPath.join(delimiter)
}
// o might not be an object when called recursively
if(Object(o) === o) {
let keys = keyPath.split(delimiter);
let key = keys.shift();
if(o.hasOwnProperty(key)) {
if(keys.length) {
// there are more keys to check, call with attribute and remaining keys
return getPath(o[key], keys.join(delimiter), delimiter);
} else {
// no more keys to check and object does have property
return o[key];
}
}
// didn't early return from having the key above, object does not have property
return undefined;
} else if(keyPath.length === 0) {
// o is not an object, but there is no remaining keyPath, so we will assume we've unwound the stack
return o;
}
// not an object and keyLength is non-zero, object does not contain property
return undefined;
};
let myObject = {
"property": {
"subproperty": {
"targetproperty": "Hi, We done it!"
}
}
};
console.log(getPath(myObject, "property:subproperty:targetproperty", ":"));
You can loop through your parts array, accessing the value of each key in each iteration.
function valueFromPath(obj, path) {
for (var i = 0; i < path.length; ++i) {
obj = obj[path[i]];
}
return obj;
};
valueFromPath(myObject, parts);
You probably want to clone the object first, in case you are going to use it for something else.
Alternatively, you can use traverse. Specifically traverse#getpath.
traverse(myObject).get(parts);
You may do as follows;
function getNestedValue(o,...a){
var val = o;
for (var prop of a) val = typeof val === "object" &&
val !== null &&
val[prop] !== void 0 ? val[prop]
: undefined;
return val;
}
let myObject = {
"property": {
"subproperty": {
"targetproperty": "Hi, We done it!"
}
}
};
let myString = "property:subproperty:targetproperty";
console.log(getNestedValue(myObject, ...myString.split(":")));
I am trying to create a function that take an object and a key whose value is an array and returns the object only with the array values that are greater than ten at that key. Otherwise, it should just return empty arrays. I think this works, but I keep getting told it doesn't.
function getElementsGreaterThan10AtProperty(obj, key) {
var greaterThan10 = [];
if (!(key in obj)) {
return greaterThan10;
} else if (obj[key].length === 0) {
return greaterThan10;
} else {
for (i = 0; i < obj[key].length; i++) {
if ((obj[key][i]) > 10) {
greaterThan10.push(obj[key][i]);
}
}
obj[key] = greaterThan10
return obj
}
}
I think the title explains it well enough. I've got an array that has two values per object and I need to look up the object by one of those values then assign a third to it.
Here are the guts:
$slides.push({
img: el.attr('href'),
desc: el.attr('title').split('Photo #')[1]
});
Which builds an array as such:
Object
desc: 127
img: img/aaron1.jpg
Object
desc: 128
img: img/aaron2.jpg
I'd like to look up the desc value, then assign a third value of in: yes
$slides.findInArray('desc', '127').addValueToObject('in','yes')
http://jsfiddle.net/S3cpa/
var test = [
{
desc: 127,
img: 'img/aaron1.jpg',
},
{
desc: 128,
img: 'img/aaron2.jpg',
}
];
function getObjWhenPropertyEquals(prop, val)
{
for (var i = 0, l = test.length; i < l; i++) {
// check the obj has the property before comparing it
if (typeof test[i][prop] === 'undefined') continue;
// if the obj property equals our test value, return the obj
if (test[i][prop] === val) return test[i];
}
// didn't find an object with the property
return false;
}
// look up the obj and save it
var obj = getObjWhenPropertyEquals('desc', 127);
// set the new property if obj was found
obj.in = obj && 'yes';
easy way
for (var i = 0; i < $slides.length; i++)
{
if ($slides[i]["desc"] == "TEST_VALUE")
{
$slides[i]['in']='yes';
}
}
Another way
Array.prototype.findInArray =function(propName,value)
{
var res={};
if(propName && value)
{
for (var i=0; i<this.length; i++)
{
if(this[i][propName]==value)
{
res = this[i];
break;
}
}
}
return res;
}
Object.prototype.addValueToObject =function(prop,value)
{
this[prop]=value;
}
---Using It--
$slides.findInArray('desc', '127').addValueToObject('in','yes');
http://jsfiddle.net/s6ThK/
You need to run it through a for loop
// Loop through the array
for (var i = 0 ; i < $slides.length ; i++)
{
// Compare current item to the value you're looking for
if ($slides[i]["desc"] == myValue)
{
//do what you gotta do
$slides[i]["desc"] = newValue;
break;
}
}
With modern JS it can be simply done:
var obj = $slides.find(e => e.desc === '127');
if (obj) {
obj.in = 'yes';
}