嗨,我有一个看起来像这样的嵌套对象
var dogTypes = {
GermanShepard {color: "black and white"},
Beagle {color: "brown and white"},
cheuwahwah {color: "green and white"},
poodle: {color: "purple and white"},
}
Run Code Online (Sandbox Code Playgroud)
我试图遍历嵌套对象中的所有属性,我知道如何使用常规对象而不是嵌套对象来执行此操作,因此如果有人可以帮助我,那就太好了。
for (var key in dogTypes) {
console.log(key + " : " + dogTypes[key])
}
Run Code Online (Sandbox Code Playgroud)
这是我打印出来的代码
GreatDane : [object Object]
GermanSheppard : [object Object]
Beagle : [object Object]
BullDog : [object Object]
Run Code Online (Sandbox Code Playgroud)
我将在哪里将颜色属性合并到 for in 循环中,请帮忙!谢谢
我正在尝试获取JavaScript数组中所有元素的列表,但我注意到使用array.toString并不总是显示数组的所有内容,即使数组的某些元素已经初始化.有没有办法在JavaScript中打印数组的每个元素,以及每个元素的相应坐标?我想找到一种方法来打印已在数组中定义的所有坐标的列表,以及每个坐标的相应值.
var coordinates = [];
coordinates[[0, 0, 3, 5]] = "Hello World";
coordinates[[0, 0, 3]] = "Hello World1";
console.log(coordinates[[0, 0, 3]]);
console.log(coordinates[[0, 0, 3, 5]]);
console.log(coordinates.toString()); //this doesn't print anything at all, despite the fact that some elements in this array are defined
Run Code Online (Sandbox Code Playgroud) {
"USA":[{"country":"Albuquerque (ABQ)"},
{"country":"Allentown (ABE)"}
],
"Canada":{"country":"Calgary (YYC)"},
"Hawaii":{"country":"Honolulu International Apt (HNL)"}
}
Run Code Online (Sandbox Code Playgroud)
这是我的对象,我需要循环它,以便我可以获得像这样的所有值
美国
卡纳德
鉴于我有一个javascript对象,有没有办法迭代所有原始子属性?
例如,如果我有一个对象
{
foo: 17,
bar: {
a: 2,
b: 7
}
}
Run Code Online (Sandbox Code Playgroud)
我想迭代foo,bar.a和bar.b.
请记住,我更喜欢迭代Object.keys()而不是使用for/in循环,尽管我确信我可以将任何for/in循环响应转换为Object.keys()迭代.
对不起,我不知道这个名字.
我想要一个函数和一个只有一个变量属性的对象.
下面是它的工作原理:
var obj = function() {
return "foo";
};
obj.prop = "bar";
obj(); // => "foo"
obj.prop; // => "bar"
Run Code Online (Sandbox Code Playgroud)
这很好,但我想改变这个顺序:
var obj = { prop: "bar" };
obj = function() {
return "foo";
};
obj(); // => "foo"
obj.prop; // => undefined
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我想这样做,因为我有很多属性要添加到对象:
var obj = function() {
return "foo";
};
obj.prop1 = "bar1";
obj.prop2 = "bar2";
obj.prop3 = "bar3";
obj.prop4 = "bar4";
obj.prop5 = "bar5";
obj.prop6 = "bar6";
obj.prop7 = "bar7";
//...
Run Code Online (Sandbox Code Playgroud) 我在javascript中有下一个代码.我删除了一些不必要的项目,因为它很长.
var options = {
dhcode: true,
commands: {
bold: {
enabled: true,
view: true,
exec: true,
cmd: 'bold',
param: null
},
italic: {
enabled: true,
view: true,
exec: true,
cmd: 'italic',
param: null
},
underline: {
enabled: true,
view: true,
exec: true,
cmd: 'underline',
param: null
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在options.commands对象中获取al数据.但我尝试的一切都不起作用.这就是我想要的:
for(var i=0;i<options.commands.length;i++) {
alert(options.commands[i].cmd);
}
Run Code Online (Sandbox Code Playgroud)
请帮我.
你好我有一个javascript问题,我得到一个JSON结果:
{"0":"San Diego acls","1":"San Diego pals","2":" San Diego CPR","3":" Temecula acls","4":" Temecula pals"}
Run Code Online (Sandbox Code Playgroud)
它存储在一个名为data的变量中.
我想解析这个数据变量并列出如下列表:
San Diego acls, San Diego pals, San Diego CPR, Temecula acls, Temecula pals
Run Code Online (Sandbox Code Playgroud)
任何优雅的方式?
谢谢
我上课的第三天,我们应该制作一个使用物体的汽车瞄准器。我有四个对象。我想为每个人制作一张表,其键位于左侧,其值位于右侧。四张桌子并排。我更愿意循环遍历按键来制作 并循环遍历按键,因为我一直在玩并且惨败。任何帮助将不胜感激。我有四个并排的 div,我想用表格填充它们。这是我的对象:
以下是我的目标和价值观:
let Accord = new Car("Accord", 22500, "Crystal White", "alloy", "bose", "leather", "tinted");
let CRV= new Car("CR-V", 28400, "Midnight Black", "alloy", "stock", "cloth", "no tint");
let Pilot = new Car("Pilot", 36796, "Modern Gray Metallic", "alloy", "bose", "leather", "tinted");
let Fit = new Car("Fit", 17499, "Raspberry Blue", "steel", "stock", "cloth", "no tint");
//Here is the entire object:
class Car extends Vehicle {
wheels: string;
stereo: string;
seatsMaterial: string;
tint: string;
constructor(model, price, color, wheels, stereo, seatsMaterial, tint) { …Run Code Online (Sandbox Code Playgroud) 在下面的 JavaScript 代码中,
obj = {};
// This work as intented
obj['a'] = { item1: 'a1', item2: 'a2' };
console.log(obj);
// Object.keys() works too
console.log(Object.keys(obj));
// forEach does not, why? and how to fix?
console.log('forEach');
obj.forEach(o => console.log(o));
Run Code Online (Sandbox Code Playgroud)
forEach 工作需要什么?
我正在将 firebase 与 axios 一起使用。我需要转换这个对象:
{r1: "Room 1", r2: "Room 2", r3: "Room 3"}
Run Code Online (Sandbox Code Playgroud)
进入数组:
rooms = [
{ id: 'r1', name: 'Room 1'},
{ id: 'r2', name: 'Room 2'},
{ id: 'r3', name: 'Room 3'},
];
Run Code Online (Sandbox Code Playgroud)
目前,我从 firebase 中使用 axious 进行调用,如下所示:
axios.get('firebaseURL').then(response => {console.log(response.data)});
Run Code Online (Sandbox Code Playgroud) 我正在进行ajax调用,我得到的响应是这样的:
Object {
Monday noon: Array[4],
Tuesday morning: Array[2],
Friday noon: Array[3],
Sunday: Array[1]
}
Run Code Online (Sandbox Code Playgroud)
我想做的事情如下:
response.length
Run Code Online (Sandbox Code Playgroud)
但我得到的回报是 undefined
知道怎样才能获得Object数组的长度?
另一个问题是:我怎么能到达阵列Monday noon: Array[4]?任何帮助或教程链接将不胜感激.
javascript ×11
object ×3
arrays ×2
properties ×2
axios ×1
for-loop ×1
function ×1
html ×1
iteration ×1
jquery ×1
json ×1
loops ×1
nested ×1
typescript ×1