Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I want to convert this:
data: {
"3": {
name: ["Missing data for required field."],
},
"5": {
id: ["Same id exist."],
},
}
into this:
data: [{
key: "name",
message: "Missing data for required field.",
row: 3,
},
{
key: "id",
message: "Same id exist.",
row: 5,
},]
how to achieve this with just es6 syntax
You can do this with Object.entries:
const data = {
"3": {
name: ["Missing data for required field."]
},
"5": {
id: ["Same id exist."]
}
};
Object.entries(data).map(([a, b]) => {
return {
key: Object.keys(b)[0],
message: Object.values(b)[0][0],
row: a
};
});
I've made some assumptions:
There will only be one key in each object
The value of the key will be an array with one string
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
let filtered = [
[
"name",
"contains",
"men"
],
[
"web",
"contains",
"www.google.com"
],
[
"phoneNumber",
"contains",
"017XXXXXXXXXX"
]
];
Expected result:
let filtered = {
name: {
contains: "men"
},
web: {
contains: "www.google.com"
},
phoneNumber: {
contains: "017XXXXXXXXXX"
}
}
I need to filtered upper array elements to object elements using javascript. Here array value need to object key & value
let filteredObject = Object.create(null);
for(let filter of filtered) {
filteredObject[filter[0]] = Object.create(null);
filteredObject[filter[0]][filter[1]] = filter[2];
}
Object.create(null) may be omitted and replaced with {} if you are certain that a trusted entity is in control of the strings in the filtered array.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have json object like this
[
{
"tag": "search"
},
{
"tag": "test"
},
{
"tag": "css"
},
]
But i want to get this object like this
'search',
'test',
'css',
How do I do this?
You can map over the array and extract the tag property value:
const obj = [ { "tag": "search" }, { "tag": "test" }, { "tag": "css" }, ]
const res = obj.map(e=>e.tag)
console.log(res)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have database which is array of objects. It look like this:
export const BIKES: Bike[] = [
{
id: 1,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/10/Krypton_A18_2016_red_ultegra_16_1.jpg',
price: 28000,
discount: 71,
main: true,
shop: 'Canada Bike',
name: 'Argon 18',`
},
{
id: 2,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/03/etap.shadowpsd_2048x.png',
price: 7900,
discount: 41,
main: false,
shop: 'Amazon',
name: 'Aquila Cycles',
}
I want to subscribe to array of objects which will consist of 2 properties 'name' and 'shop' and theirs values.
It can be implemented using Array.map
const result = BIKES.map(({name, shop}) => ({ name, shop }));
console.log(result);
or
const result = BIKES.map((item) => {
return {
name: item.name,
shop: item.shop
};
});
console.log(result);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data like:
var data = [
{
items: [
{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [
{
id: 123
},
{
id: 234
}
]
}
]
so, I want count object deep in array inside of all data by property 'id'.
ex: data.countObject('id',123) //return 3.
and my data have about xx.000 item, which solution best?
Thanks for help (sorry for my English)
You can use reduce & forEach. Inside the reduce callback you can access the items array using curr.items where acc & curr are just parameters of the call back function. Then you can use curr.items.forEach to get each object inside items array
var data = [{
items: [{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [{
id: 123
},
{
id: 234
}
]
}];
function getCount(id) {
return data.reduce(function(acc, curr) {
// iterate through item array and check if the id is same as
// required id. If same then add 1 to the accumulator
curr.items.forEach(function(item) {
item.id === id ? acc += 1 : acc += 0;
})
return acc;
}, 0) // 0 is the accumulator, initial value is 0
}
console.log(getCount(123))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I got an array of objects of the following form:
let postArray = [
{
id = 1,
body = "some text"
},
{
id = 2,
body = "some other text"
}
]
which I want to rearange to the following form:
{
posts: {
1: {
id = 1,
body = "some text"
},
2: {
id = 2,
body = "some other text"
}
}
}
How can I do that using ES6?
You can reduce your array:
let postArray = [
{
id: 1,
body: "some text"
},
{
id: 2,
body: "some other text"
}
]
const posts = postArray.reduce((acc, b) => ({...acc, [b.id]: b}), {})
const result = { posts }