gpt4 book ai didi

javascript - es6 代码在 es5 中被破坏

转载 作者:行者123 更新时间:2023-11-28 13:15:09 24 4
gpt4 key购买 nike

由于工作中的一些框架限制,我一直在尝试将我的代码从 es6 转换为 es5...尽管我一直在努力找出问题所在。由于某种原因,代码的工作方式不太一样,并且也没有错误......

有人可以告诉我我的翻译是否正确吗?

这是 ES6 代码:

function filterFunction(items, filters, stringFields = ['Title', 'Description'], angular = false) {
// Filter by the keys of the filters parameter
const filterKeys = Object.keys(filters);

// Set up a mutable filtered object with items
let filtered;

// Angular doesn't like deep clones... *sigh*
if (angular) {
filtered = items;
} else {
filtered = _.cloneDeep(items);
}

// For each key in the supplied filters
for (let key of filterKeys) {
if (key !== 'TextInput') {
filtered = filtered.filter(item => {

// Make sure we have something to filter by...
if (filters[key].length !== 0) {
return _.intersection(filters[key], item[key]).length >= 1;
}

return true;
});
}

// If we're at TextInput, handle things differently
else if (key === 'TextInput') {
filtered = filtered.filter(item => {
let searchString = "";

// For each field specified in the strings array, build a string to search through
for (let field of stringFields) {
// Handle arrays differently
if (!Array.isArray(item[field])) {
searchString += `${item[field]} `.toLowerCase();
} else {
searchString += item[field].join(' ').toLowerCase();
}
}

// Return the item if the string matches our input
return searchString.indexOf(filters[key].toLowerCase()) !== -1;
});
}
}
return filtered;
}

这是我翻译的代码,部分 99% 有效..

function filterFunction(items, filters, stringFields, angular) {
// Filter by the keys of the filters parameter
var filterKeys = Object.keys(filters);

// Set up a mutable filtered object with items
var filtered;

// Angular doesn't like deep clones... *sigh*
if (angular) {
filtered = items;
} else {
filtered = _.cloneDeep(items);
}

// For each key in the supplied filters
for (var key = 0 ; key < filterKeys.length ; key ++) {

if (filterKeys[key] !== 'TextInput') {
filtered = filtered.filter( function(item) {

// Make sure we have something to filter by...
if (filters[filterKeys[key]].length !== 0) {
return _.intersection(filters[filterKeys[key]], item[filterKeys[key]]).length >= 1;
}

return true;
});
}

// If we're at TextInput, handle things differently
else if (filterKeys[key] === 'TextInput') {
filtered = filtered.filter(function(item) {
var searchString = "";

// For each field specified in the strings array, build a string to search through
for (var field = 0; field < stringFields.length; field ++) {
// Handle arrays differently
console.log(field);
if (!Array.isArray(item[stringFields[field]])) {
searchString += item[stringFields[field]] + ' '.toLowerCase();
} else {
searchString += item[stringFields[field]].join(' ').toLowerCase();
}
}

// Return the item if the string matches our input
return searchString.indexOf(filters[filterKeys[key]].toLowerCase()) !== -1;
});
}
}
return filtered;
}

最佳答案

这两行

searchString += `${item[field]} `.toLowerCase();
searchString += item[stringFields[field]] + ' '.toLowerCase();

确实不等同。要对字符串的所有部分应用 toLowerCase 方法,您需要将 ES5 连接括在括号中:

searchString += (item[stringFields[field]] + ' ').toLowerCase();

或者,由于空格无论如何都不能小写,因此只需使用

searchString += item[stringFields[field]].toLowerCase() + ' ';

关于javascript - es6 代码在 es5 中被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39115376/

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