Insert object into dynamic key nested array [duplicate] - javascript

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

Related

Redefine object properties keys starting with prefix [duplicate]

This question already has answers here:
What's the quickest way to rename all property names in a Javascript object?
(3 answers)
How to remove part of a string?
(7 answers)
JavaScript: Object Rename Key
(35 answers)
Closed 2 months ago.
If I have the following object, how can I redefine the keys to remove the prefix "Class"?
{
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
so it becomes
{
Name: "John",
Email: "john#doe.com",
Phone: "1234567"
}
Is there any easy way?
const obj = {
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
const newObj = Object.entries(obj).reduce((acc, item) => ({ ...acc,
[item[0].replace(/Class/g, "")]: item[1]
}), {})
console.log(newObj)

How to make custom function that can access object items? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Want to know how we can access the items in an object when passed to a function :
const sortArrayObj = (arrayObj, field) => {
//How to access "arrayObj[0].field" ???
// Tried to do console.log(`${arrayObj[0]}`) and it gave output as [Object object]
//Now how can i access "name" and "date" from it using the "field" variable ?
};
const arr = [
{ name: "A", date: "13-12-1996" },
{ name: "G", date: "03-02-2020" },
{ name: "C", date: "09-05-2021" },
];
sortArrayObj(arr,"date")
Try in the following way:
const sortArrayObj = (arrayObj, field) => {
// How to access "arrayObj[0].field" ???
arrayObj[0][field];
};

Javascript: How do you extract an object from an array and then store it in a serparated object? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How to get the first element of an array?
(35 answers)
Closed 2 years ago.
This may be a very simple question, but I have not found the answer in the last hour. I have the following array, which always contains a single object in index 0. For convenience in later use, I want to remove the object from the array and store it directly as a single object. I.e. the object should no longer be wrapped in the array.
Current Status: Array
Array(1)
0:
bio: "Test"
id: 2
image: "http://localhost:8000/media/default.jpg"
user: 2
Target: Object
Object
bio: "Test"
id: 2
image: "http://localhost:8000/media/default.jpg"
user: 2
You can just assign the first value of the array to a variable. Remember, the array just stores a reference to the object, this reference can be stored in a variable instead and interacted with that way.
var arr = [
{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
}
]
var obj = arr[0] // obj stores a reference to the object in arr
console.log(obj)
If you want to work with the object outside of the array without modifying the object in the array, you can "duplicate" it with the spread operator.
var arr = [
{
bio: "Test",
id: 2,
image: "http://localhost:8000/media/default.jpg",
user: 2
}
]
var obj = {...arr[0]} // obj stores a reference to an object identical to arr[0]
console.log(obj)

Javascript return true if a value exists in a nested array [duplicate]

This question already has answers here:
Check if a value exists in an Array object in JavaScript or Angular
(4 answers)
Closed 3 years ago.
I want to return true if a nested array contains a particular value
In this example I'm trying to see if the users array has the current users id but I get the object instead of true
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.find(user=>user.id === currentUserId))
The problem is you are using .find() instead of .some(). Try the following:
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.some(user=>user.id === currentUserId))
The difference is in the output. .find() will return the value, .some() will return a boolean.

How to delete Object from deep javascript array? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 6 years ago.
I want to delete object from my array posted below but nothing works. If I delete the properties it just goes to undefined. I have a recursive function that returns to me the right object. I wish to delete the second object and his children.
var data = {
parameters: [
{
id: 0,
name: "First",
value: "1",
children: []
},
{
id: 1,
name: "Second",
value: "2",
children: [
{
id: 2,
name: "Third",
value: "3",
children: []
}
]
}
],
index: 3
}
You could use Array#splice.
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
data.parameters.splice(1, 1)
// index // \\ length

Categories