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 7 months ago.
Improve this question
I am trying to access certain objects that are inside a parsed JSON and stored inside an object. In this case data.json
var data = {
json: '',
text: '',
welcome: '',
q1: '',
}
let foo = await fetch(spreadsheet);
data.text = await foo.text();
data.json = JSON.parse(data.text.substr(47).slice(0, -2))
data.welcome = data.json.table.rows[2]
console.log(data.welcome)
I don't know how to access each of them to be able to save them in the object I have.
Here is the parse JSON:
{
c: [
null,
{ v: '2. Pagos' },
{
v: '1. Mi pago no concuerda. (Recuerda que el reporte debe estar dentro de la fecha especificada)'
},
{ v: 'Link Formulario' },
{ v: 'https://forms.gle/jBnmFyHCCBePN9Ws5' },
null,
{ v: null }
]
}
If this:
JSON.parse(data.text.substr(47).slice(0, -2))
works fine, this:
console.log(data.json.c[1].v)
should print:
Pagos
Related
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; }
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);
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);
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 5 years ago.
Improve this question
My current object is of the form:
[{Vol:'AB', freq:[{used:4786, avail:1319, res:249}]}
,{Vol:'CD', freq:[{used:1101, avail:412, res:674}]}]
I need to change it to the form:
[{Vol:'AB', freq:{used:4786, avail:1319, res:249}}
,{Vol:'CD', freq:{used:1101, avail:412, res:674}}]
How can this be done.
Try
obj.map( s => (s.freq = s.freq[0], s) );
Explanation
Iterate the array using map
Replace each items freq's array with its first item
Demo
var input = [{
Vol: 'AB',
freq: [{
used: 4786,
avail: 1319,
res: 249
}]
}, {
Vol: 'CD',
freq: [{
used: 1101,
avail: 412,
res: 674
}]
}];
input = input.map(s => (s.freq = s.freq[0], s));
console.log(input);
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.