gpt4 book ai didi

需要两个输入的 javascript 函数 : an object and a key and returns the corresponding value for that key within the object

转载 作者:行者123 更新时间:2023-12-02 14:38:06 25 4
gpt4 key购买 nike

我正在尝试创建一个 JavaScript 函数,该函数接受一个对象作为输入,并返回该对象中所有键的值。请帮助检查我的代码,我没有得到我期望的结果。

var james = {
firstName: “James”,
lastName: “John”,
age: 40,
job: “programmer”,
married: false
};

function hasElObject(valObject, key) {
var i = valObject.length;
while (i--) {
if (valObject[i] === key) {
return valObject[i];
}
}
return ‘key not found’;
}
var objectEl = hasElObject (['congratulations', 'top', 8, 90], 5);
log (arrayEl);

var retValue = getValue(mary,email);
log (retValue);

最佳答案

function hasElObject(valObject, key) {
for (var k in valObject) {
if (k == key)
return valObject[k];
}
return 'key not found';
}

var jobOfJames = hasElObject(james, "job");
console.log(jobOfJames);

关于需要两个输入的 javascript 函数 : an object and a key and returns the corresponding value for that key within the object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com