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 last month.
Improve this question
I have an array of words in a .json file
Like this:
[
"city",
"river",
"car"
]
And i want to get an object for each word
Something like this:
[
{
word: "city",
something: "..."
},
{
word: "river",
something: "..."
},
{
word: "car",
something: "..."
}
]
What is the best way to do this?
let new_array = [];
for(let item of old_array)
{
new_array.push({word:item, something:"..."});
}
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 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 9 years ago.
Improve this question
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.
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.
Improve this question
I have an array of objects like this :
[
{
"event" : {
"teams": "bla bla",
}
},
{
"event" : {
"teams": "bla bla",
}
},
]
How to combine all events that have the same teams value into one array like this:
[
{
"event" : [
{
"teams" : "bla bla"
},
{
"teams": "bla bla"
},
]
}
]
I can only use lodash
This would convert the first object objectArray into the other.
const combinedArray = [{ event: objectArray.map(obj => obj.event) }];
Edit: Grouping by teams
const groupedArray = [];
const teams = new Set(objectArray.map(obj => obj.event.teams).filter(n=>n));
for (const team of teams)
groupedArray.push({ event: objectArray.map(obj=>obj.event).filter(event => event.teams === team)});
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 1 year ago.
Improve this question
I have an array like this:
0: {id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red"}
And i need to modify it to have like that:
{id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red"}
If you need to unwrap the first value, you can destructure it fairly easy. Make sure the variable is a let or var and not a const.
let data = [{ id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red" }];
[data] = data; // Destructure the first item (unwrap the first result)
console.log(data); // Print the modified object.