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

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);

Related

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; }

How to sort array of objects with unique keys by nested object value [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
Really struggling to find a solution here. I have the following data:
var arr = [
{ Main: {type: "object", ...} }
{ Poller: {type: "object", ...} }
{ connection: {type: "array", ...} }
{ Profile1: {type: "array", ...} }
{ Sender: {type: "object", ...} }
{ Profile2: {type: "array", ...} }
];
My goal is to generate an array that lists the objects of type: array first. I tried various implementations of the sort() method but don't know how to account for each nested object having its' own key. Any help is appreciated
Like this
var arr = [
{ Main: {type: "object",}},
{ Poller: {type: "object",}},
{ connection: {type: "array" ,}},
{ Profile1: {type: "array" ,}},
{ Func1: {type: "function",}},
{ func2: {type: "function",}},
{ Sender: {type: "object",}},
{ Profile2: {type: "array" ,}}
];
arr.sort((a, b) => {
if (Object.values(a)[0].type < Object.values(b)[0].type) return -1
if (Object.values(a)[0].type === Object.values(b)[0].type) return 0
if (Object.values(a)[0].type > Object.values(b)[0].type) return 1
})
console.log(arr)

How to iterate over these array 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
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.

How to create array out of object entries? [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 need help creating a JavaScript object:
I have three fields from the input form in React state object:
{
calories: "45",
fats: "56",
proteins: "67"
}
I would like to create a new array i.e. nutrientInformation from this data. The output should resemble this:
nutrientInformation: [
{
calories: 45
},
{
fats: 56
},
{
proteins: 67
}
]
const state = { calories: "45", fats: "56", proteins: "67" };
const nutrientInformation = Object.entries(state).map(([key, value]) => ({
[key]: value
}));
console.log(nutrientInformation);

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