This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed last month.
I would like to convert array to a list like :
This :
[ RowDataPacket { id_name: '' } ]
To :
[ '' ]
I just want a list, whitout name, no 'id_name'
Create a new empty array and when looping your rowDataPacket array, push them to new array.
Example;
// your array = rowDataPackets =[{id_name:''},{id_name:''},...]
const CleanArray = [];
rowDataPackets.forEach( ({ id_name : idName }) => { CleanArray.push(idName) });
console.log(CleanArray);
// CleanArray = ['','',...]
You can use Map Array method to iterate over this arr and get the key
const result = [ RowDataPacket { id_name: '' } ].map( item => item.id_name )
Related
This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 1 year ago.
I might need some help on JavaScript about array stuff.
[
"URL_1",
"URL_2",
"URL_3",
"URL_4",
...,
"URL_30"
]
Let's just say these above are an array of strings which are links and how do I move them to an array object like this below?
[
{
url: "URL_1"
},
{
url: "URL_2"
},
{
url: "URL_3"
},
...,
{
url: "URL_30"
}
]
Using Array#map:
const arr = [ "URL_1", "URL_2", "URL_3", "URL_4", "URL_30" ];
const res = arr.map(url => ({ url }));
console.log(res);
simply:
let temp = [];
yourArray.foreach((val) =>{
temp.push({val})
})
yourArray = temp;
I have an array of object as below
const result = [
{ email: 'me#example.com' },
{ email: 'c#examples.com' }
];
Expected Output :
"me#example.com, c#examples.com"
How can i get the desired output?
result.map(({email}) => email).join(', ')
iterate thru the array and join it.
i'm new here, i have problem that i can not solve.
I have 2 different arrays:
The first array - contains ratings of users with their ID name
[
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
]
The second set - contains the ratings, which I want to return to each user.
If there is a rating that is not in the second set, I will not return it
In the second set, values do not matter, only keys
[
"_assembler",
"_css",
"_python",
"_php"
]
I want to return to the first set, the handle, and all the rankings that exist in the second set.
[
{"handle":"frontend1", "_python":"3" },
{"handle":"frontend3", "_php":"4", "_python":"5" },
{"handle":"frontend4"}
]
this is what i try to do.
keys = [
"_assembler",
"_css",
"_python",
"_php"
]
source = [
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
];
result = [];
tmp = {};
source.forEach((item) => {
Object.keys(item).map(({key,value}) =>
{
if(key == "handle")
{
tmp[key]=value;
}
if(keys.includes(key))
{
tmp[key]=value;
}
})
result.push(...tmp);
tmp = {};
});
You can do this with a map utilizing a couple of other array methods such as filter, and Object methods.
const keys = [
"_assembler",
"_css",
"_python",
"_php"
]
const source = [
{"handle":"frontend1", "_redis":"3", "_nodejs":"5", "_mysql":"2", "_python":"3", "_mongo":"4"},
{"handle":"frontend3", "_php":"4", "_mysql":"4", "_oracle":"4", "_ruby":"3", "_mongo":"5", "_python":"5"},
{"handle":"frontend4", "_java":"5", "_ruby":"5", "_mysql":"5", "_mongo":"5"}
];
const result = source.map( s => ({
handle: s.handle,
...Object.fromEntries(Object.entries(s).filter(x => x[0] != "handle" && keys.includes(x[0])))
}));
console.log(result);
This question already has answers here:
Create an object with dynamic property names [duplicate]
(2 answers)
Closed 2 years ago.
I have an array of objects with a string value in it, with a first array as an header:
var data = [{records : "productId*amount*tax1*tax2*tax3"},
{records : "111*2000*10*12*13"},
{records : "113*3000*10**"}]
I need to convert this array of objects into an individual objects with key-value pairs by using the data[0] as key and rest of the data[x] as its value .
Expected output :
data: [
{
"productId" : "111",
"amount" : '2000",
"tax1" : "10",
"tax2" : "12",
"tax3" : "13"
},
{
"productId" : "113",
"amount" : "3000",
"tax1" : "10",
"tax2" : "0",
"tax3" : "0"
}
]
I can use the split operator to split the strings and get them as an individual array, but can't figure out how to assign it to key value pairs in an array and I'm very new to JS.
So you split the first index into the keys.
You loop over the remaining and generate the objects using the key for the index.
var data = [{
records: "productId*amount*tax1*tax2*tax3"
},
{
records: "111*2000*10*12*13"
},
{
records: "113*3000*10**"
}
]
// grab the keys from first record
var keys = data.shift().records.split("*")
// loop over the rest of the records
var result = data.reduce(function(arr, item) {
// split up the records
const rec = item.records.split("*").reduce(function(obj, value, index) {
// grab the key for the index
var key = keys[index]
// set the object with the key and the value
obj[key] = value
// return back the updated object
return obj
}, {})
// add the object to the array
arr.push(rec)
return arr
}, [])
console.log(result);
You can use Object.fromEntries in combination with normal iteration methods, like .map:
let data = [{records : "productId*amount*tax1*tax2*tax3"},
{records : "111*2000*10*12*13"},
{records : "113*3000*10**"}];
let [fields, ...rows] = data.map(({records}) => records.split("*"));
let result = rows.map(values =>
Object.fromEntries(values.map((value, i) => [fields[i], value]))
);
console.log(result);
This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 5 years ago.
const cars = [
{
'id': 'truck',
'defaultCategory': 'vehicle'
}
]
const output = []
Object.keys(cars).map((car) => {
output.push({
foo: cars[car].defaultCategory
})
})
console.log(output)
This work fine, however what I want to achieve is so that the newly crated object has structure of 'truck': 'vehicle'.
So if I replace push argument with
${cars[car].id}`: cars[car].defaultCategory
I get SyntaxError: Unexpected template string
What am I doing wrong?
Use map on the array, and not the keys (the indexes) to get an array of objects. For each object use computed property names to set the id value as the key:
const cars = [
{
'id': 'truck',
'defaultCategory': 'vehicle'
}
];
const result = cars.map(({ id, defaultCategory }) => ({ [id]: defaultCategory }));
console.log(result);
You should use .map() over your cars array and not Object.keys(cars):, we don't use Object.keys() with arrays.
This is how should be your code:
var output = cars.map(function(car) {
return {
[car.id]: car.defaultCategory
};
});
var cars = [{
'id': 'truck',
'defaultCategory': 'vehicle'
}];
var output = cars.map(function(car) {
return {
[car.id]: car.defaultCategory
};
});
console.log(output);