简体   繁体   English

编写一个 function 称为键,它接受 object 并返回 object 中所有键的数组

[英]Write a function called keys, which accepts an object and returns an array of all of the keys in the object

Beginner at JavaScript. JavaScript 的初学者。 I can't use the Object.keys() function.我不能使用 Object.keys() function。 This is what I have so far.这就是我到目前为止所拥有的。 I can't for the life of me figure out how to return all the keys in the object, it's only returning "['a']", the first key:我无法弄清楚如何返回 object 中的所有键,它只返回“['a']”,第一个键:

const keys = (obj) => {
var obj1 = {a: 1, b: 2, c: 3};
var newArray = [];

for(var key in obj1) {
  newArray.push(key);
  return newArray;
}

};

keys();

Your loop will only run once because your return statement is inside the loop, which terminates your entire function, including the loop.您的循环只会运行一次,因为您的 return 语句在循环内,这会终止您的整个 function,包括循环。

Madhawa Priyashantha is right: put the return statement after the loop and it should work. Madhawa Priyashantha 是对的:将 return 语句放在循环之后,它应该可以工作。

The return statement inside the for loop terminates it after the first iteration so you should put the return statement after the loop as shown below. for循环内的 return 语句在第一次迭代后终止它,因此您应该将 return 语句放在循环之后,如下所示。

 const keys = (obj) => { var obj1 = { a: 1, b: 2, c: 3 }; var newArray = []; for (var key in obj1) { newArray.push(key); } return newArray; }; keys();

Facetiously not using Object.keys() - you can use Object.entries() and map() and to create an array of the first item in the key: value pairings.开玩笑地不使用 Object.keys() - 您可以使用 Object.entries() 和 map() 并创建键中第一项的数组:值配对。

Presumably this is some sort of homework or teaching activity - the reason for my post is to show that there are many ways to achieve the outcome and some are just different ways of saying the same thing.大概这是某种家庭作业或教学活动——我发帖的原因是为了表明有很多方法可以达到结果,有些只是说同一件事的不同方式。

The obvious answer is to use a loop as per your attempt and the other solutions - but my solution is intended to invite you to see things in a different way.显而易见的答案是根据您的尝试和其他解决方案使用循环 - 但我的解决方案旨在邀请您以不同的方式看待事物。 And putting arbitary limits on the way you can achieve something forces you to adopt either inefficient proactise or prevents you from using newer methodologies.对实现某事的方式进行任意限制会迫使您采用低效的主动或阻止您使用更新的方法。

Rather than forcing you to use a loop - a better teaching activity would be to get you to use the newer javascript methods- especially map(), reduce() and filter().与其强迫你使用循环——更好的教学活动是让你使用更新的 javascript 方法——尤其是 map()、reduce() 和 filter()。

There are definitely reasons to get you to learn loops - but doing so in an exercise to get the keys of an object limits your ability to use current code approaches and is just wrong.绝对有理由让您学习循环 - 但在练习中这样做以获取 object 的密钥会限制您使用当前代码方法的能力,而且是错误的。 IMO.国际海事组织。

FYI - I am an ex-teacher and believe in using the right learning activity and the right learning tools to achieve the learning.仅供参考 - 我是一名前教师,相信使用正确的学习活动和正确的学习工具来实现学习。

 const keys = (obj) => { return Object.entries(obj).map(item => item[0]); }; const obj = {a: 1, b: 2, c: 3}; console.log(keys(obj)); // gives ["a", "b", "c"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Write a function called values, which accepts an object and returns an array of all of the values in the object w/o using Object.values() - Write a function called values, which accepts an object and returns an array of all of the values in the object w/o using Object.values() 该函数以数组的形式返回对象的所有键或值 - A function returning all keys or values of an object as array 为什么将 function object 传递给 Object.keys() 返回空数组? - Why passing function object to Object.keys() returns empty array? 用键和数组打字稿写一个对象 - write an object with keys and array typescript 编写一个接受参数和对象的jQuery函数 - write a jquery function which accepts params and object 编写一个函数以返回对象中任何级别的所有键 - Write a function to return all the keys present in an object at any level 使用Object.keys返回整数数组 - Using Object.keys returns integer array GraphQL 错误“字段必须是以字段名称作为键的对象或返回此类对象的函数。” - GraphQL error "fields must be an object with field names as keys or a function which returns such an object." 获取JSON解析对象中的所有键/数组键? - Get all Keys/Array keys in JSON parse Object? Javascript:确定数组中的所有元素是否都是对象中的键 - Javascript: Determine if all of the elements in the array are keys in the object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM