获取我的 JSON 对象 Angularjs 的键值
Get key value of my JSON object Angularjs
我的对象
object1 ={
name: xxx,
Id: 212
}
我需要这样的输出:
{
212:xxx
}
谁能帮我做一下?
for(var key in object1) {
if(object1.hasOwnProperty(key))
{
console.log( eachPeriod[key]);
}
}
您可以将带有用括号括起来的变量的对象的key
设置为[Id]
。 convert()
函数以对象为参数,通过destructuring将name
和Id
的值赋给相应的变量。然后,使用这些变量构造对象。
const obj = {
name: 'xxx',
Id: 212
};
function convert ({ name, Id }) {
return {
[Id]: name
};
}
console.log(convert(obj));
我的对象
object1 ={
name: xxx,
Id: 212
}
我需要这样的输出:
{
212:xxx
}
谁能帮我做一下?
for(var key in object1) {
if(object1.hasOwnProperty(key))
{
console.log( eachPeriod[key]);
}
}
您可以将带有用括号括起来的变量的对象的key
设置为[Id]
。 convert()
函数以对象为参数,通过destructuring将name
和Id
的值赋给相应的变量。然后,使用这些变量构造对象。
const obj = {
name: 'xxx',
Id: 212
};
function convert ({ name, Id }) {
return {
[Id]: name
};
}
console.log(convert(obj));