Home > Net >  Java Script combine strings/arrays
Java Script combine strings/arrays

Time:07-02

I need to combine string1 and string2 like first result. After that, I only want the unique information, without "color type information" like the second result.

string1 = "Green | Green | Red | Orange | Blue | Cut | Yellow | Yellow"

string2 = "Color | Color | Color | Color | Color | Technical | Color | Color"

First result: string3 = Green Color | Green Color | Red Color | Orange Color | Blue Color | Cut Technical | Yellow Color | Yellow Color

Second result: string4 = Green | Red | Orange | Blue | Cut | Yellow

CodePudding user response:

you can do something like this

const string1 = 'Green | Green | Red | Orange | Blue | Cut | Yellow | Yellow'
const string2 = 'Color | Color | Color | Color | Color | Technical | Color | Color'

const [arr1, arr2] = [string1, string2].map(s => s.split(' | '))


const string3 = arr1.map((s, i) => `${s} ${arr2[i]}`).join(' | ')
const string4 = [...new Set(arr1)].join(' | ')
console.log({string3, string4})

CodePudding user response:

const string1 = 'Green | Green | Red | Orange | Blue | Cut | Yellow | Yellow'
const string2 = 'Color | Color | Color | Color | Color | Technical | Color | Color'

const combine = (str1, str2, div = ' | ') => {
  str1 = str1.split(div)
  str2 = str2.split(div)
  
  return str1.map((word, i) => `${word} ${str2[i]}`).join(div)
}

const unique = str1 => [...new Set(str1.split(' | '))].join(' | ')

const firstResult = `Green Color | Green Color | Red Color | Orange Color | Blue Color | Cut Technical | Yellow Color | Yellow Color`
const secondResult = 'Green | Red | Orange | Blue | Cut | Yellow'

console.log(firstResult === combine(string1, string2))
console.log(secondResult === unique(string1))

CodePudding user response:

You could do this:

string1 = "Green | Green | Red | Orange | Blue | Cut | Yellow | Yellow"

split_text = string1.split("|")
Result = Array.from(new Set(split_text.map(x => x.trim()))).join('|')
//'Green|Red|Orange|Blue|Cut|Yellow'

CodePudding user response:

If string1 and string2 information length varies

var string1 = "Green | Green | Red | Orange | Blue | Cut | Yellow | Yellow";

var string2 = "Color | Color | Color | Color | Color | Technical | Color | Color";

var firstlength=string1.split("|").length;
var secondlength=string2.split("|").length;
var maxilength=firstlength;
if(secondlength>firstlength)
  maxilength=secondlength;
 
 var string1split=string1.split("|");
 var string2split=string2.split("|");
 
 var concatstr="";
 for(var i=0;i<maxilength;i  ){
 if(string1split.length>i){
   concatstr =string1split[i];
 }
 if(string2split.length>i){
   concatstr =string2split[i];
 }
 if(i<maxilength-1){
   concatstr ="|";
 }
}
 
console.log(concatstr);
 
function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}
for(var i=0;i<string1split.length;i  ){
string1split[i]=string1split[i].trim();
}
var uniquecolors=string1split.filter(onlyUnique);
var uniquecolorsstring=uniquecolors.join(" | ");
console.log(uniquecolorsstring);
 

  • Related