I am using angularjs where I have a $scope.var1 ={a:10, b:20, c:30}; in which I want to append another value(s) which is infact an array of objects i.e. $scope.myobjects=[{m:10, n:30}, {x:6, y:8}, ....]; after appending this value my $scope.var1 should look like, $scope.var1={a:10, b:20, c:30, m:10, n:30, x:6, y:8};
any idea please.
Thanks
obj ={a:10, b:20, c:30};
arr=[{m:10, n:30}, {x:6, y:8}];
arr.forEach(function(a){
Object.keys(a).forEach(function(key){
obj[key]=a[key];
})
})
console.log(obj);
You could iterate the array and use Object.assign.
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
If not on the user agent available, like on IE, you could use a polyfill.
var target = { a: 10, b: 20, c: 30 };
objects = [{ m: 10, n: 30 }, { x: 6, y: 8 }];
objects.forEach(function (o) {
Object.assign(target, o);
});
console.log(target);
Object.assign.apply(null, [$scope.var1].concat($scope.myobjects))
Please try this one:
var var1 = { a: 10, b: 20, c: 30 };
myobjects = [{ m: 10, n: 30 }, { x: 6, y: 8 }];
myobjects.forEach(function (o) {
for (var p in o) {var1[p] = o[p]};
});
console.log(var1);
Note that this code simply add/update properties based on source object
Related
I need a function that would make an array like this from an object:
let obj = {
name: 'john',
age: 25,
some: {
a: 5.5,
b: 5,
c: 'pls'
}
}
objectToArray(obj);
// Outputs: [['name', 'john'], ['age', 25], ['some', [['a',5.5], ['b', 5], ['c','pls']]]]
How exactly should I check if there is a nested object? Is the Object.entries () method a good fit?
I think we need a function that will check whether the object is passed or not, if so, it will open it and make an array of it [key, property] using the Object.entries () method, if there is also an object, then it will call itself again and for it ...
Ps: i need to use only native javascript with recursion
We can recurse like this:
Check if the object passed in is an object using instanceof.
If it is continue.
If not return whatever the value is.
Convert the object to it's entries using Object.entries.
Iterate over each [key, value] pair using Array#map.
Recursively call objectToArray on the value. (Jump to 1.)
This is what that looks like:
const obj = { name: 'john', age: 25, some: { a: 5.5, b: 5, c: 'pls', }, };
function objectToArray(obj) {
if (!(obj instanceof Object)) return obj;
return Object.entries(obj).map(([key, value]) => [key, objectToArray(value)]);
}
console.log(objectToArray(obj))
For example I want something like:
{
a: 1,
b: 2,
c: 3
}
turned into:
{
d: {
a: 1,
b: 2,
c: 3
}
}
I've tried assigning a new property to that object with the object itself but it shows up as circular so I figure it's a reference instead of the actual properties instead of the actual values. I want to try something like JSON.stringify the object and assign it to the property but I don't know how to turn that string into an object format that I can assign to the property.
let firstObj = {
a: 1,
b: 2,
c: 3
}
let secondObj = {};
secondObj.d = firstObj;
console.log(secondObj);
Basically you create a new object and assign the original object to its property d.
You can use ES6 destructuting to make a shallow copy of the object and put it on a new prop:
let obj = {
a: 1,
b: 2,
c: 3
}
obj.d = {...obj}
console.log(obj)
If that's not an option you can reduce() over the objects keys to make a new object and assign it to d:
let obj = {
a: 1,
b: 2,
c: 3
}
obj.d = Object.keys(obj).reduce((newObj, k) => {
newObj[k] = obj[k]
return newObj
},{})
console.log(obj)
It depends whether you want to make the deep or shallow copy of the object d. (Can the object d have a nested structure?)
The question about efficient ways to clone the object has already been answered here.
I was looking into Javascript .every documentation and it have a second parameter which is passed the description says
Optional. Value to use as this when executing the callback. If a thisArg parameter is provided to every, it will be used as callback's this value. Otherwise, the value undefined will be used as its this value. The this value ultimately observable by the callback is determined according to the usual rules for determining the this seen by a function.
which means that I can access that values as this in the callback function, what is a practical example of this?
As per MDN,
Optional. Value to use as this when executing callback. [Ref]
It means, you can use provided value as this in callback function to test the value.
const data = [0, 1, 2, 3];
const doesPass = data.every(function(el) {
return el < this.toTest;
}, {
toTest: 4
});
console.log(doesPass);
Every iteration method of Array contains thisArg as second parameter. This is an example for filtering object keys with another object by using a prototype, which usually requires an object, which is handed over as argument, or in the second a bound to the method.
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).filter(Object.prototype.hasOwnProperty, o2);
}
console.log(intersection(firstObject, secondObject));
The same with previously bound o2.
var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 },
secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 };
function intersection(o1, o2) {
return Object.keys(o1).filter(Object.prototype.hasOwnProperty.bind(o2));
}
console.log(intersection(firstObject, secondObject));
Is there any way to copy an object with lodash, but not all properties.
The only way I know is manually copying it property by property
wanted e.g.:
var obj = {
a: 'name',
b: [1,2,3],
c: {
z: 'surname',
x: []
},
d: {
y: 'surname2',
w: []
}
};
and the result be like
var copy_obj = {
b: [1,2,3],
c: {
z: 'surname',
x: []
}
};
Edit:
I finally opted for:
var blacklist = ['a','d'];
_.cloneDeep(_.omit(obj, blacklist));
The omit serves almost this exact purpose:
_.cloneDeep(_.omit(obj, blacklist));
Fiddle here: https://jsfiddle.net/c639m9L2/
You could use the pick function:
_.pick(obj, 'b', 'c')
You can use the second parameter to JSON.stringify to do this.
JSON.parse(JSON.stringify(obj, ['b', 'c']))
You can use a combination of assign and pick
Object.assign(copy_obj, _.pick(obj, ['b', 'c']));
In this way if copy_obj has other properties you don't override them.
var blacklist = ['a','d'];
_.cloneDeep(_.omit(obj, blacklist));
I have the following array objects
var stats = [
[0, 200,400], [100, 300,900],[220, 400,1000],[300, 500,1500],[400, 800,1700],[600, 1200,1800],[800, 1600,3000]
];
I would like to know how to convert it to the following JavaScript object.
var stats = [
{x:0, y:200,k:400}, {x:100, y:300,k:900},{x:220, y:400,k:1000},{x:300, y:500,k:1500},{x:400, y:800,k:1700},{x:600, y:1200,k:1800},{x:800, y:1600,k:3000}
];
Array.prototype.map is what you need:
stats = stats.map(function(x) {
return { x: x[0], y: x[1], k: x[2] };
});
What you describe as the desired output is not JSON, but a regular JavaScript object; if it were JSON, the key names would be in quotes. You can, of course, convert a JavaScript object to JSON with JSON.stringify.
You can use map()
var stats = [
[0, 200,400], [100, 300,900],[220, 400,1000],[300, 500,1500],[400, 800,1700],[600, 1200,1800],[800, 1600,3000]
];
stats = stats.map(function(el) {
return {
x: el[0],
y: el[1],
k: el[2]
};
});
console.log(stats);