Merge 2 objects with similar prop [duplicate] - javascript

This question already has answers here:
How to deep merge instead of shallow merge?
(47 answers)
Closed 5 years ago.
I want to merge 2 objects where there's a similar prop on both and I want to have all props together:
let obj1 = {'foo': 'bar', 'far': {'tst': 1}}
let obj2 = {'far': {'other': 'token'}}
Object.assign({},obj1, obj2);
// Outputs: {"foo":"bar","far":{"other":"token"}}
// Desired output: {"foo":"bar","far":{'tst': 1, "other":"token"}}
Do I need to use the spread operator in some way?

Do you try it?
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

Related

Remove duplicate content in array from another array [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 4 months ago.
In Javascript, I have this
var a = ["1","2","3"]
var b = ["3","4","5"]
assuming "b" reads "a" and removes the 3 because it is repeated, how can I get this?
var c = ["4","5"]
Thank You!
You can use set and spread operators.
set has property to have all unique value object.
Below we are using Set constructor passing values using spread operator and it return an object so to get an array we are again passing that object with spread operator.
let a = ["1", "2", "3"]
let b = ["3", "4", "5"]
let c = [...new Set([...a, ...b])];
console.log(c);
Check if value exists on another array using includes()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can remove values by using filter()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
To do what you want:
const a = ["1","2","3"]
const b = ["3","4","5"]
const c = b.filter((value) => !a.includes(value))
This has been answered before and is in detail: How to get the difference between two arrays in JavaScript?

get object value from an array [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 months ago.
Im quite new to front-end development. I am using React.
My problem:
How to extract object value that are within array?
myArr = [[{a: 1, b: 2}], [{a: 1, b:2}], [{a: 1, b:2}]]
// I want to extract only the values of b and make them into array
// my asnwer should look like this:
// [2,2,2]
My Approach:
const [answerArr, setAnswerArr] = useState([]);
useEffect(() => {
const extractCode = () => {
const res = myArr.map((item)=>{
????
})
};
extractCode();
}, [codeArr]);
I've tried using map method...but I am struggling very much...
If you could help me i would learn so much!
Your code should look something like this
const res = myArr.map(item => item[0].b)

How to quickly assign {x:1,y:2} to {row,col}? [duplicate]

This question already has an answer here:
ES6/ES2015 object destructuring and changing target variable
(1 answer)
Closed 3 years ago.
I want to quickly assign {x:1,y:2} to {row,col}, i.e. let row = objXY.x, let col = objXY.y.
I know the destructuring assignment can do this
let {x,y} = objXY
But is there any way to do like this?
let {row,col} = objXY
You can assign destructured properties to new variable names as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names
For example
const obj = {x: 1, y: 2};
const {x: row, y: col} = obj;
console.log(row); // 1
console.log(col); // 2

optimal solution to filter elements from array [duplicate]

This question already has answers here:
Remove all elements contained in another array [duplicate]
(17 answers)
Closed 5 years ago.
I have an array of animals arr = ['cat','dog','elephant','lion','tiger','mouse']
I want to write a function remove(['dog','lion']) which can remove the elements from arr, and returns a new array, what is the best and optimal solution?
example:
arr = ['cat','dog','elephant','lion','tiger','mouse']
remove(['cat', 'lion'])
arr should get changed to
arr = ['dog','elephant','tiger','mouse']
Note: No Mutations
you can just use filter()
var arr = ['cat','dog','elephant','lion','tiger','mouse'];
var newArr = arr.filter(x => !['cat', 'lion'].includes(x))
console.log(newArr);

Add an element to a multidimensional object? [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
var John = { Cats: 2, Dogs: 3, Turtles: 1 };
var Mary = { Dogs: 0, Parakeets: 3};
How do I append new dimensions after I've already created the objects?
...John now also has 1 Parakeet
...Mary now also has 5 Koi
Just give them a value, like:
John.Parakeet = 1;
You can then later access these properties just like any other properties.
It really isn't hard.

Categories

Resources