输出一个包含函数的数组

时间:2016-09-14 13:15:08

标签: javascript html

所以我想制作一段以一组变量开头的代码,然后使用.sort()和.length()按字母顺序排序然后编号,然后输出结果,任何想法?到目前为止,我已经想出了这个,但它只输出

下的内容

`

<h1>task 2</h1>

<script>
var products ["Router","Tablet","Printer","Nachos","sandwhich"];
document.write(products.sort());
document.write(prod caucts.length);
</script>

`

1 个答案:

答案 0 :(得分:0)

尝试:

<script>
var products=["Router","Tablet","Printer","Nachos","sandwhich"];
products.sort();
var enumerated_products=[];
products.forEach(function(x,i,arr){enumerated_products.push({x,i})});
console.log(enumerated_products);
</script>