我希望在另一个数组中返回带有索引的数组元素。例如:
#!/usr/bin/env node
var hmin = require('html-minifier');
var fs = require('fs');
var src = fs.readFileSync(process.argv[2]).toString();
var res = hmin.minify(src, {
removeComments: true,
removeCommentsFromCDATA: true,
collapseWhitespace: true,
conservativeCollapse: true,
minifyJS: true,
preventAttributesEscaping: true,
minifyJS: true,
customAttrAssign: [ /\$=/ ]
});
fs.writeFileSync(process.argv[3], res);
通常我会使用:
array = ["h", "y", "R", "X", "y", "u", "w", "o", "q"]
indices_array = [1, 3, 7, 8]
我需要做更多这样的事情(因为我的indices_array很长)
array.values_at(1, 3, 7, 8)
答案 0 :(得分:8)
您可以使用splat operator:
array.values_at(*indices_array)
示例输出:
array = ["h", "y", "R", "X", "y", "u", "w", "o", "q"]
indices_array = [1, 3, 7, 8]
array.values_at(*indices_array)
# => ["y", "X", "o", "q"]
答案 1 :(得分:1)
class Array
def value_at(a)
values_at(*a)
end
end