我正在使用Adobe ExtendScript Toolkit进行调试。我正在为InDesign编写代码,实际上并没有丰富的JavaScript知识(也不知道ES6的含义)。 ExtendScript不接受let作为保留字。也许您想表达的有关ES6的一些信息?
我需要比较数组中的所有项目,如果其中一个子项相同,则将其中的一些项加入。让我解释一下:
基于以下数组:
var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]
所以,我想得到这个数组的结果:
var array = [[1,"luis"] , [2,"felipe; erick; mark"] , [3,"dustin"]]
如何比较数组项以获得所需的结果?
我已经尝试了两个for循环和一段时间。 也许我弄错了。因此,我在这里问你们。
如果我使用这个:
for (var i=0; i<array.length-1; i++) {
for (var j=i+1; j<array.length; j++) {
if (array[i][0] == array[j][0]) {
array[i][1] = array[i][1] + "; " + array[j][1];
}
}
}
我有所有结果。像这样:
1,luis
2,felipe; erick; mark
2,erick; mark
2,mark
3,dustin
答案 0 :(得分:1)
使用Object.entries
和reduce
。
var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];
const res = Object.entries(array.reduce((a, [n, s]) => (a[n] = a[n] ? a[n] + "; " + s : s, a), {}));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
答案 1 :(得分:0)
您可以使用reduce
var array = [
[1, "luis"],
[2, "felipe"],
[2, "erick"],
[2, "mark"],
[3, "dustin"]
]
const result = array.reduce(function(re, arr) {
const item = re.find(function(o) {
return o[0] === arr[0]
})
typeof item === 'undefined' ? re.push(arr) : (item[1] = item[1] + ';' + arr[1])
return re
}, [])
console.log(result)
答案 2 :(得分:0)
您可以使用tf.function
和Array的Object.entries()
方法来获得所需的输出:
.reduce()
const data = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];
const result = Object.entries(data.reduce((r, [key, val]) => {
r[key] = key in r ? [r[key].toString() + "; " + val] : val;
return r;
}, {}));
console.log(result);
答案 3 :(得分:0)
您可以使用reduce和map
var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]
let op = array.reduce((op,inp)=>{
op[inp[0]] = op[inp[0]] || []
op[inp[0]].push(inp[1])
return op
},{})
let final = Object.entries(op).map(([key,value])=> [+key,value.join('; ')])
console.log(final)
答案 4 :(得分:0)
这是下面的解决方法
var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]]
function mergeArrayBySameId(){
let old = [];
let mergedArr = [];
array.map(function(curr, i){
let tmp = "";
let current = array[i];
let prev = ( i === 0 ) ? [] : array[i-1];
if( current[0] === prev[0] ){
tmp = current[0];
mergedArr[0] = tmp;
if( !mergedArr.includes( prev[1] ) ){
mergedArr.push( prev[1] );
}
mergedArr.push( current[1] );
}
old = prev;
});
let mergedStr = ""
let mergedArrId = mergedArr[0];
mergedArr.map(function(val){
if( typeof val !== 'number' ){
mergedStr += val + "; ";
}
});
mergedArr = [];
mergedArr[0] = mergedArrId;
mergedArr[1] = mergedStr;
function checkId(id){
return mergedArr[0] != id[0];
}
let filterArray = array.filter(checkId);
let finalArray = filterArray.concat([mergedArr]).sort();
return finalArray;
}
console.log( mergeArrayBySameId() );
答案 5 :(得分:0)
谢谢大家的建议。 很抱歉,因为我是一个初学者,并且在为Adobe软件自动化进行编码时只熟悉javascript。 我找到了一种使用方法:
var array = [[1,"luis"] , [2,"felipe"] , [2,"erick"] , [2,"mark"] , [3,"dustin"]];
for (var i=0; i<array.length-1; i++) {
var j = i+1;
while (j < array.length) {
if (array[i][0] == array[j][0]) {
array[i][1] = array[i][1] + "; " + array[j][1];
array.splice(j , 1);
j = i;
}
j++;
}
}
alert(array.join("\r"));
答案 6 :(得分:0)
let arr = [
[1, "luis"],
[3, "felipe"],
[2, "erick"],
[3, "mark"],
[3, "dustin"]
];
let res = arr.reduce((acc,cur) => {
const item = acc.find((obj) => {
if(obj[0] === cur[0]){
obj.push(cur[1]);
return obj
}
});
if(!item)
acc.push(cur);
return acc
},[]);