How to iterate over these array objects? [closed] - javascript

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
let arr = [{
text: "first",
nodes: [{
text: "a1"
}, {
text: "a2"
}]
},
{
text: "Second",
nodes: [{
text: "b1"
}]
}, {
text: "Third",
nodes: [{
text: "c1"
}]
}
]
arr.map((el, index) => {
console.log(el.nodes[index])
})
How can I iterate over all the nodes which has different size of array objects.
I've tried iterating over them, but the index keeps incrementing and skip past the nodes

You can loop in a loop... like:
let answerObj=[];
arr.forEach(item=>{
let nodeList=[];
item.nodes.forEach(el => {
nodeList.push(el)
})
answerObj.push({ text: item.text, nodes: nodeList})
})
console.log(answerObj)
And this is what you get: Output
And you can output by [index] as well if you want.

Related

Change array elements [closed]

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 8 months ago.
Improve this question
There is an array
[ [ 'Alex', '167%' ],
[ 'Benjamin', '127%' ],
[ 'Elijah', '117%' ],
[ 'Liam', '136%' ],
[ 'Theodore', '135%' ],
[ 'Mia', '128%' ] ]
I need to make it look like this
[ 'Alex 167%',
'Benjamin 127%',
'Elijah 117%',
'Liam 136%',
'Theodore 135%',
'Mia 128%' ]
I need the elements of each nested array to be combined into a string and such a number of spaces are inserted between them so that the length of this string is 29 characters
You could use a map combined with padEnd
const items = [
['Alex', '167%'],
['Benjamin', '127%'],
['Elijah', '117%'],
['Liam', '136%'],
['Theodore', '135%'],
['Mia', '128%']
];
const combined = items.map(([name, percent]) =>
name.padEnd(29 - percent.length, ' ') + percent
);
console.log(combined);

delete and modify properties in array inside json object [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 12 months ago.
Improve this question
Given the following JSON:
const myJson = {
id: 8,
active: true,
invoice: "2022/1001",
invoiceDate: "2022-02-02",
clientId: 1,
storeId: 1,
total: 76.95,
itens: [
{
id: 11,
quantity: 2,
price: 12.10,
total: 24.20,
productId: 1,
invoiceId: 8
},
{
id: 12,
quantity: 5,
price: 10.55,
total: 52.75,
productId: 2,
invoiceId: 8
}
]
};
I need a simple way to remove two attributes from the each item inside the 'itens' array, the properties are 'id' and 'invoiceId'. I also need to recalculate the 'total', coz I do not trust totally the source of the information, I rather multiply 'quantity' by 'price' myself.
I've produced this rather naive code:
myJson.itens.forEach(item => {
item.total =
item.quantity *
item.price;
delete item.id;
delete item.invoiceId;
});
And it works rather fine, however, no way that it must be it. Looks too lame and laborious to me. I'm exhausted googling for better ways of doing it.
Can it be done better?
Rather than mutating the original data, you could map it to a new object
const myJson = {"id":8,"active":true,"invoice":"2022/1001","invoiceDate":"2022-02-02","clientId":1,"storeId":1,"total":76.95,"itens":[{"id":11,"quantity":2,"price":12.1,"total":24.2,"productId":1,"invoiceId":8},{"id":12,"quantity":5,"price":10.55,"total":52.75,"productId":2,"invoiceId":8}]}
const newObject = {
...myJson, // keep the rest but map "itens"
itens: myJson.itens.map(({ id, invoiceId, ...iten }) => ({
...iten, // everything except "id" and "invoiceId"
total: iten.quantity * iten.price // recalc total
}))
}
console.log("newObject:", newObject)
.as-console-wrapper { max-height: 100% !important; }

JS: How to filter object property and became key [closed]

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 2 years ago.
Improve this question
I have a object like this
"field": {
"zh":{
"game_code" : "游戏",
"transfer_amount" : "金额",
"uni_id" : "流水号",
"create_time" : "建立时间"
},
"en": {
"game_code" : "GameCode",
"transfer_amount" : "Amount",
"uni_id" : "UniqId.",
"create_time" : "CreateTime"
}
}
}
and I want to format data like this
data: [
{
game_code: {
zh: '游戏',
en: 'GameCode'
},
},
{
transfer_amount: {
zh: '金额',
en: 'Amount'
}
}
]
what is the best way to do this ? thanks
(I edited that I wanna format data code)
Firstly, get all keys for your result data, and then loop through data.betlog_field for language key? Here is the code:
const data: any = {
"betlog_field": {
"zh": {
"game_code": "游戏",
"transfer_amount": "金额",
"uni_id": "流水号",
"create_time": "建立时间"
},
"en": {
"game_code": "GameCode",
"transfer_amount": "Amount",
"uni_id": "UniqId.",
"create_time": "CreateTime"
}
}
};
let result: any = { data: [] };
console.clear();
let keys = Object.keys(data.betlog_field.zh);
keys.forEach(key => {
let item: any = {};
Object.keys(data.betlog_field).forEach((lan: any) => {
item[lan] = data.betlog_field[lan][key];
});
result.data.push({[key]: item});
});
console.log(result);

How to restructure objects of data into arrays of objects? [closed]

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 currently have 1 array that has 6 names of the category and 6 arrays of objects, each of which includes 5 objects for each category. What's the best way to restructure the 1-category array and 6-arrays of objects so that I can get just 6 arrays of objects contain both data from the array.
let categoryArray = ["a", "b", "c","d","e","f"];
let outcomeArrays = [
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ],
[ {p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null} ]
];
What I want is to make an array that has such a structure like below:
[{category: "a",
details: [{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}]},
{category: "b",
details: [{p:11,q:22,r:null},{p:33,q:44,r:null},{p:55,q:66,r:null},{p:77,q:88,r:null},{p:9,q:10,r:null}]},
...
]
I tried to use push and map method. It didn't work. I even tried to use lodash and can't find a method dealing with this situation.
The version from #thoughtsunificator looks right.
This is a variation using Array.map:
let categoryArray = ["a", "b", "c","d","e","f"];
let outcomeArray = [[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],
[{p:1,q:2,r:null},{p:3,q:4,r:null},{p:5,q:6,r:null},{p:7,q:8,r:null},{p:9,q:10,r:null}],];
const categories = categoryArray.map((cat, index) => ({
category: cat,
details: outcomeArray[index]
}));
console.log(categories);

how to remove a record in an array or object by knowing the value of the record to be removed [closed]

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 have an Array as following
[
{ id: "172~~81259", title: "Absurdity (Theatre)" },
{ id: "2803~~281318", title: "Academic Administration" },
{ id: "3722~~296974", title: "Accessories Design" },
{ id: "23843", title: "Accommodation and Front Office Professional" },
{ id: "16834", title: "Accountant" },
{ id: "853~~64400", title: "Accounting Information System" },
{ id: "4403~~40149", title: "Activation and Experiential Advertising" },
{ id: "15~~39301", title: "Activation and Experiential Advertising" }
]
I want to filter(remove) already selected values from this array.so how can i filter by knowing the value only.
You need array.filter. It generates a new array containing only the items which cause the supplied callback to return true. Use that callback to check the value you want to check, whatever that value is.

Categories

Resources