用于访问 javascript 中的 json 键的函数的参数,节点
a parameter of a function to access a json key in javascript , node
我有一个包含 10 个元素的 json: {"id":"2","name":"Peter","number":"A4584857","father":"Gerart","color":"green"}
我尝试修改 json 所以我有一个像这样读写的函数:
function updatefilelocal(id,texto) {
const fs = require('fs');
fs.readFile('students.json', (err, data) => {
if (err) throw err;
let json = JSON.parse(data);
console.log('This is after the read call')
const filename='students.json';
var file=json;
file.alumnos[id].color= texto
fs.writeFile(filename,JSON.stringify(file),function writeJson(err){
if (err)return console.log(err);
//console.log(JSON.stringify(file));
//console.log('writing to'+filename )
});
});
}
所以,用这一行: file.alumnos[id].color= texto
和这个功能: updatefilelocal(2,'yellow')
我可以修改我的 json 中的关键颜色,嗯,它有效但是当我想修改我的元素的键 'name' 我必须像这样更改名称的颜色:file.alumnos[id].name= texto
没问题,但我想做的是在我的函数中添加第三个参数:
function updatefilelocal(id,texto,third_parameter) {...}
在 'name' 上更改 'color'。我会这样调用我的函数:updatefilelocal(id,texto,name)
如果我想更改我的元素的名称或
updatefilelocal(id,texto,color)
如果我想更改我的元素的颜色...我已经尝试了所有...我已经放了括号 file.alumnos[id].{third parameter}= texto
但没有任何效果...
感谢您的帮助。
您可以将密钥作为第三个参数传入,并使用[key]
访问它。所以你的代码中的一个例子是:
function updatefilelocal(id, texto, key) {
// Your code
file.alumnos[id][key] = texto;
// Rest of your code
}
您可以通过使用 updatefilelocal(id, texto, 'color')
或 updatefilelocal(id, texto, 'name')
调用 updatefilelocal
来选择更改颜色或名称。
下面是一个例子,看看它是如何工作的:
let x = { a: 1, b: 2 };
function changeKeyValue(obj, key, val) {
obj[key] = val;
}
changeKeyValue(x, 'a', 3); // x = { a: 3, b: 2 }
changeKeyValue(x, 'b', 4); // x = { a: 3, b: 4 }
我有一个包含 10 个元素的 json: {"id":"2","name":"Peter","number":"A4584857","father":"Gerart","color":"green"}
我尝试修改 json 所以我有一个像这样读写的函数:
function updatefilelocal(id,texto) {
const fs = require('fs');
fs.readFile('students.json', (err, data) => {
if (err) throw err;
let json = JSON.parse(data);
console.log('This is after the read call')
const filename='students.json';
var file=json;
file.alumnos[id].color= texto
fs.writeFile(filename,JSON.stringify(file),function writeJson(err){
if (err)return console.log(err);
//console.log(JSON.stringify(file));
//console.log('writing to'+filename )
});
});
}
所以,用这一行: file.alumnos[id].color= texto
和这个功能: updatefilelocal(2,'yellow')
我可以修改我的 json 中的关键颜色,嗯,它有效但是当我想修改我的元素的键 'name' 我必须像这样更改名称的颜色:file.alumnos[id].name= texto
没问题,但我想做的是在我的函数中添加第三个参数:
function updatefilelocal(id,texto,third_parameter) {...}
在 'name' 上更改 'color'。我会这样调用我的函数:updatefilelocal(id,texto,name)
如果我想更改我的元素的名称或
updatefilelocal(id,texto,color)
如果我想更改我的元素的颜色...我已经尝试了所有...我已经放了括号 file.alumnos[id].{third parameter}= texto
但没有任何效果...
感谢您的帮助。
您可以将密钥作为第三个参数传入,并使用[key]
访问它。所以你的代码中的一个例子是:
function updatefilelocal(id, texto, key) {
// Your code
file.alumnos[id][key] = texto;
// Rest of your code
}
您可以通过使用 updatefilelocal(id, texto, 'color')
或 updatefilelocal(id, texto, 'name')
调用 updatefilelocal
来选择更改颜色或名称。
下面是一个例子,看看它是如何工作的:
let x = { a: 1, b: 2 };
function changeKeyValue(obj, key, val) {
obj[key] = val;
}
changeKeyValue(x, 'a', 3); // x = { a: 3, b: 2 }
changeKeyValue(x, 'b', 4); // x = { a: 3, b: 4 }