This question already has answers here:
Remove specific properties from Array objects in Node.js
(7 answers)
Closed 2 days ago.
The community is reviewing whether to reopen this question as of 2 days ago.
let objArr = [ { "name" : "Rohan", "request": true}, { "name" : "Sohan", "request": true, "modify" : "today", "expire": "tomorrow"}, { "name" : "Mohan", "request": true, "modify" : "today", "expire": "tomorrow"} ];
const newArr = objArr.map(v => ({ ...v, oldName: v.name, newName: v.name + '_copy', newRecord: true }))
console.log(newArr)
How to remove expire, modify, from the array?
Only oldName, newName, newRecord and request should display, the rest we can disable/remove.
Rather than deleting some attributes, you can copy only those properties which are required as shown below
const newArr = objArr.map(v => {oldName: v.name, newName: v.name, newRecord: true})
Related
This question already has answers here:
Get value from object using 'Array Path'
(3 answers)
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 months ago.
array = ['data', 'category', 'hour'];
object = {
"status": {
"type": "INFO",
"messages": []
},
"data": {
"id": 1,
"tenant": "675832",
"process": "6911d872-35f8-11ea-8697-001dd8b71c20",
"category": "resquests"
"time": {
hour: "12",
minute: "30"
}
}
I need to check if object has keys with same value contained in array.
I tried split array by dot, and then filter both array and object but it fails.
const array = inputValue.split('.').map((item) => item);
Do you need something like that?
console.log(arrayEquals(Object.keys(object),array)
with
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I get this response from REST API.
"items": [
{
"name": "one"
},
{
"name": "two"
}
]
I want to reduce it to array ["one", "two"]. Can someone please tell me how?
const res = {
items: [{
name: 'one'
},
{
name: 'two'
}
]
};
console.log(res.items.map(res => res.name));
try this
using map
var items = [
{
name: "one",
},
{
name: "two",
},
];
console.log(items.map((item) => item.name));
This question already has answers here:
How to convert an array of objects to object with key value pairs
(6 answers)
Closed 1 year ago.
I've got an object with different values .
let arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},
{"Fashion": "1300"},{"Documentary": "2600"}]
How can I foreach them and then put them in an object and pick a name for them like this :
"photo_type": {
"Personal": "1000",
"sport": "2100",
"Industrial": "1200",
"Commercial": "2300",
"Fashion": "1300",
"Documentary": "2600"
}
I dont want them to be like 0 and 1 and 2.
You could create an object with Object.assign and by spreading the array.
let array = [{ Personal: "1000" }, { sport: "2100" }, { Industrial: "1200" }, { Commercial: "2300" }, { Fashion: "1300" }, { Documentary: "2600" }],
object = Object.assign({}, ...array);
console.log(object);
You can try this
const arr = [{"Personal": "1000"},{"sport": "2100"},{"Industrial": "1200"},{"Commercial": "2300"},{"Fashion": "1300"},{"Documentary": "2600"}]
let result = {}
arr.forEach(member => {result = {...result, ...member}}
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Convert array of string to array of object using es6 or lodash
(4 answers)
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
I have an array like this:
const categories = ["Action", "Comedy", "Thriller", "Drama"]
And I would like to convert it to this format:
const displayedCategories = [{"type": "Action", "isDisplayed": true},{"type": "Comedy", "isDisplayed": true},{"type": "Thriller", "isDisplayed": true},{"type": "Drama", "isDisplayed": true}]
Any advice ? :)
You can do so by using map:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
const newCategories = categories.map(category => ({
type: category,
isDisplayed: true
}));
console.log(newCategories);
You can do so with the map method:
const displayedCategories = categories.map(function(category) {
return { type: category, isDisplayed: true };
});
Maybe like This:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
var out = [];
for(var key in categories){
out.push({"type": categories[key], "isDisplayed": true});
}
console.log(out);
use array.prototype.map to convert the array into an object
categories = catergories.map(val => {
var obj = {
type:val
//more properties go here
}
return obj
}
const categories = ["Action", "Comedy", "Thriller", "Drama"]
const displayedCategories = categories.map(category => { return {"type": category, "isDisplayed": true} })
console.log('displayedCategories :', displayedCategories);
This question already has answers here:
How to transpose a javascript object into a key/value array
(11 answers)
How to convert an object to array of objects [duplicate]
(5 answers)
Closed 3 years ago.
Need to create below output from the JSON obj
let obj = {"id" : 1, "name": "John"};
expected result:
[
{key: "id", value: "1"},
{key: "name", value: "John"}
]
You can get the keys of obj using Object.keys() and map them to objects.
const obj = {
"id": 1,
"name": "John"
};
const result = Object.keys(obj).map(k => ({
key: k,
value: obj[k]
}));
console.log(result);
You can try this:
JSON.stringify(
Object.entries({"id" : 1, "name": "John"}).map(([key, value]) => ({
key: key,
value: value
}))
)