在每次出现特定子字符串时剪切数组中存在的 JS 字符串并附加到同一数组
Cut JS string present in an array at every occurrance of a particular substring and append to same array
注:
此时此刻,我无法更好地表达问题标题。如果有人能把它说得更好,请继续!
我有:
var array = ["authentication.$.order", "difference.$.user.$.otherinformation", ... , ...]
我需要的:
["authentication", "authentication.$", "authentication.$.order",
"difference", "difference.$", "difference.$.user", "difference.$.user.$",
"difference.$.user.$.otherinformation"]
基本上,无论我在哪里看到 .$.
,我都需要保留它,然后附加 .$.
出现之前的所有内容以及 .$
[=20= 出现之前的所有内容]
示例:
difference.$.user.$.otherinformation
应解析为包含:
difference
difference.$
difference.$.user
difference.$.user.$
difference.$.user.$.otherinformation
我强烈认为这里涉及某种递归,但还没有朝那个方向发展。
下面是我的实现,但不幸的是,当我的子字符串匹配 .$.
的第一次出现时,它停止并且不会继续检查其他出现的 .$.
相同的字符串。
我怎样才能最好地结束它?
当前有缺陷的实施:
for(var i=0; i<array.length; i++){
// next, replace all array field references with $ as that is what autoform's pick() requires
// /\.\d+\./g,".$." ==> replace globally .[number]. with .$.
array[i] = array[i].replace(/\.\d+\./g,".$.");
if(array[i].substring(0, array[i].lastIndexOf('.$.'))){
console.log("Substring without .$. " + array[i].substring(0, array[i].indexOf('.$.')));
console.log("Substring with .$ " + array[i].substring(0, array[i].indexOf('.$.')).concat(".$"));
array.push(array[i].substring(0, array[i].indexOf('.$.')).concat(".$"));
array.push(array[i].substring(0, array[i].indexOf('.$.')));
}
}
// finally remove any duplicates if any
array = _.uniq(array);
您可以在数组循环中使用此函数。
var test = "difference.$.user.$.otherinformation";
function toArray(testString) {
var testArr = testString.split(".")
var tempString = "";
var finalArray = [];
for (var i = 0; i < testArr.length; i++) {
var toTest = testArr[i];
if (toTest == "$") {
tempString += ".$"
} else {
if (i != 0) {
tempString += ".";
}
tempString += toTest;
}
finalArray.push(tempString)
}
return finalArray;
}
console.log(toArray(test))
我使用正则表达式抓取所有内容,直到 .$
的最后一次出现并将其切碎,直到什么都没有。最后反转。
let results = [];
let found = true;
const regex = /^(.*)\.$/g;
let str = `difference.$.user.$.otherinformation`;
let m;
results.push(str);
while(found) {
found = false;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if(m.length > 0) {
found = true;
results.push(m[0]);
str = m[1];
}
}
}
results.push(str);
results = results.reverse();
// Concat this onto another array and keep concatenating for the other strings
console.log(results);
您只需要在数组上循环,将结果存储在临时数组中,然后将它们连接到最终数组中。
您可以按如下方式使用reduce:
const dat = ["authentication.$.order", "difference.$.user.$.otherinformation"];
const ret = dat.reduce((acc, val) => {
const props = val.split('.');
let concat = '';
return acc.concat(props.reduce((acc1, prop) => {
concat+= (concat ? '.'+ prop : prop);
acc1.push(concat);
return acc1;
}, []));
}, [])
console.log(ret);
使用如下简单的 for
循环:
var str = "difference.$.user.$.otherinformation";
var sub, initial = "";
var start = 0;
var pos = str.indexOf('.');
for (; pos != -1; pos = str.indexOf('.', pos + 1)) {
sub = str.substring(start, pos);
console.log(initial + sub);
initial += sub;
start = pos;
}
console.log(str);
其实这个问题不需要递归。您可以改用带有子循环的常规循环。
您只需要:
- 将数组中的每个事件拆分为子字符串;
- 从这些子字符串中构建一系列累加值;
- 用这个系列替换数组的当前元素。
此外,为了使替换正常工作,您必须以相反的顺序迭代数组。顺便说一句,在这种情况下,您不需要删除数组中的重复项。
所以代码应该是这样的:
var array = ["authentication.$.order", "difference.$.user.$.otherinformation"];
var SEP = '.$.';
for (var i = array.length-1; i >= 0; i--){
var v = array[i];
var subs = v.replace(/\.\d+\./g, SEP).split(SEP)
if (subs.length <= 1) continue;
var acc = subs[0], elems = [acc];
for (var n = subs.length-1, j = 0; j < n; j++) {
elems[j * 2 + 1] = (acc += SEP);
elems[j * 2 + 2] = (acc += subs[j]);
}
array.splice.apply(array, [i, 1].concat(elems));
}
console.log(array);
功能性单衬垫可能是;
var array = ["authentication.$.order", "difference.$.user.$.otherinformation"],
result = array.reduce((r,s) => r.concat(s.split(".").reduce((p,c,i) => p.concat(i ? p[p.length-1] + "." + c : c), [])), []);
console.log(result);
注: 此时此刻,我无法更好地表达问题标题。如果有人能把它说得更好,请继续!
我有:
var array = ["authentication.$.order", "difference.$.user.$.otherinformation", ... , ...]
我需要的:
["authentication", "authentication.$", "authentication.$.order",
"difference", "difference.$", "difference.$.user", "difference.$.user.$",
"difference.$.user.$.otherinformation"]
基本上,无论我在哪里看到 .$.
,我都需要保留它,然后附加 .$.
出现之前的所有内容以及 .$
[=20= 出现之前的所有内容]
示例:
difference.$.user.$.otherinformation
应解析为包含:
difference
difference.$
difference.$.user
difference.$.user.$
difference.$.user.$.otherinformation
我强烈认为这里涉及某种递归,但还没有朝那个方向发展。
下面是我的实现,但不幸的是,当我的子字符串匹配 .$.
的第一次出现时,它停止并且不会继续检查其他出现的 .$.
相同的字符串。
我怎样才能最好地结束它?
当前有缺陷的实施:
for(var i=0; i<array.length; i++){
// next, replace all array field references with $ as that is what autoform's pick() requires
// /\.\d+\./g,".$." ==> replace globally .[number]. with .$.
array[i] = array[i].replace(/\.\d+\./g,".$.");
if(array[i].substring(0, array[i].lastIndexOf('.$.'))){
console.log("Substring without .$. " + array[i].substring(0, array[i].indexOf('.$.')));
console.log("Substring with .$ " + array[i].substring(0, array[i].indexOf('.$.')).concat(".$"));
array.push(array[i].substring(0, array[i].indexOf('.$.')).concat(".$"));
array.push(array[i].substring(0, array[i].indexOf('.$.')));
}
}
// finally remove any duplicates if any
array = _.uniq(array);
您可以在数组循环中使用此函数。
var test = "difference.$.user.$.otherinformation";
function toArray(testString) {
var testArr = testString.split(".")
var tempString = "";
var finalArray = [];
for (var i = 0; i < testArr.length; i++) {
var toTest = testArr[i];
if (toTest == "$") {
tempString += ".$"
} else {
if (i != 0) {
tempString += ".";
}
tempString += toTest;
}
finalArray.push(tempString)
}
return finalArray;
}
console.log(toArray(test))
我使用正则表达式抓取所有内容,直到 .$
的最后一次出现并将其切碎,直到什么都没有。最后反转。
let results = [];
let found = true;
const regex = /^(.*)\.$/g;
let str = `difference.$.user.$.otherinformation`;
let m;
results.push(str);
while(found) {
found = false;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if(m.length > 0) {
found = true;
results.push(m[0]);
str = m[1];
}
}
}
results.push(str);
results = results.reverse();
// Concat this onto another array and keep concatenating for the other strings
console.log(results);
您只需要在数组上循环,将结果存储在临时数组中,然后将它们连接到最终数组中。
您可以按如下方式使用reduce:
const dat = ["authentication.$.order", "difference.$.user.$.otherinformation"];
const ret = dat.reduce((acc, val) => {
const props = val.split('.');
let concat = '';
return acc.concat(props.reduce((acc1, prop) => {
concat+= (concat ? '.'+ prop : prop);
acc1.push(concat);
return acc1;
}, []));
}, [])
console.log(ret);
使用如下简单的 for
循环:
var str = "difference.$.user.$.otherinformation";
var sub, initial = "";
var start = 0;
var pos = str.indexOf('.');
for (; pos != -1; pos = str.indexOf('.', pos + 1)) {
sub = str.substring(start, pos);
console.log(initial + sub);
initial += sub;
start = pos;
}
console.log(str);
其实这个问题不需要递归。您可以改用带有子循环的常规循环。
您只需要:
- 将数组中的每个事件拆分为子字符串;
- 从这些子字符串中构建一系列累加值;
- 用这个系列替换数组的当前元素。
此外,为了使替换正常工作,您必须以相反的顺序迭代数组。顺便说一句,在这种情况下,您不需要删除数组中的重复项。
所以代码应该是这样的:
var array = ["authentication.$.order", "difference.$.user.$.otherinformation"];
var SEP = '.$.';
for (var i = array.length-1; i >= 0; i--){
var v = array[i];
var subs = v.replace(/\.\d+\./g, SEP).split(SEP)
if (subs.length <= 1) continue;
var acc = subs[0], elems = [acc];
for (var n = subs.length-1, j = 0; j < n; j++) {
elems[j * 2 + 1] = (acc += SEP);
elems[j * 2 + 2] = (acc += subs[j]);
}
array.splice.apply(array, [i, 1].concat(elems));
}
console.log(array);
功能性单衬垫可能是;
var array = ["authentication.$.order", "difference.$.user.$.otherinformation"],
result = array.reduce((r,s) => r.concat(s.split(".").reduce((p,c,i) => p.concat(i ? p[p.length-1] + "." + c : c), [])), []);
console.log(result);