对象和我的代码
Objects and my code
var friends = {
bill: {
firstName: 'bill',
lastName: 'Green',
number: ('812381293'),
address : ['chicken st 12','rigth corner']
}
steve: {
firstName: 'steve',
lastName: 'Brown',
number: ('812222381293'),
address : ['chicken st 12','rigth corner']
}
};
var list = function(friends) {
for (var name in friends) {
console.log(name);
}
}
var search = function (friends) {
for (var key in friends) {
if ( friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
}
我有语法错误:
Unexpected identifier
我的代码有什么问题?
这是我在当前任务中需要做的事情:
Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it.
您缺少一个逗号:
}, // <--- this!
steve: {
并将函数的参数更改为name
,因为你应该寻找它。
var search = function (name) { // change friends to name!
for (var key in friends) {
if ( friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
}
工作代码:
var friends = {
bill: {
firstName: 'bill',
lastName: 'Green',
number: ('812381293'),
address: ['chicken st 12', 'rigth corner']
},
steve: {
firstName: 'steve',
lastName: 'Brown',
number: ('812222381293'),
address: ['chicken st 12', 'rigth corner']
}
},
list = function (friends) {
for (var name in friends) {
console.log(name);
}
},
search = function (name) {
for (var key in friends) {
if (friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
};
document.write('<pre>' + JSON.stringify(search('steve'), 0, 4) + '</pre>');
var friends = {
bill: {
firstName: 'bill',
lastName: 'Green',
number: ('812381293'),
address : ['chicken st 12','rigth corner']
}
steve: {
firstName: 'steve',
lastName: 'Brown',
number: ('812222381293'),
address : ['chicken st 12','rigth corner']
}
};
var list = function(friends) {
for (var name in friends) {
console.log(name);
}
}
var search = function (friends) {
for (var key in friends) {
if ( friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
}
我有语法错误:
Unexpected identifier
我的代码有什么问题?
这是我在当前任务中需要做的事情:
Define a function search that takes a single argument, name. If the argument passed to the function matches any of the first names in friends, it should log that friend's contact information to the console and return it.
您缺少一个逗号:
}, // <--- this!
steve: {
并将函数的参数更改为name
,因为你应该寻找它。
var search = function (name) { // change friends to name!
for (var key in friends) {
if ( friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
}
工作代码:
var friends = {
bill: {
firstName: 'bill',
lastName: 'Green',
number: ('812381293'),
address: ['chicken st 12', 'rigth corner']
},
steve: {
firstName: 'steve',
lastName: 'Brown',
number: ('812222381293'),
address: ['chicken st 12', 'rigth corner']
}
},
list = function (friends) {
for (var name in friends) {
console.log(name);
}
},
search = function (name) {
for (var key in friends) {
if (friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
};
document.write('<pre>' + JSON.stringify(search('steve'), 0, 4) + '</pre>');