0

I have string that input from user like this var str="aa bb cc"

Can I need like this

mylists = [ 
           { "0": "11", "1": "aa" }, 
           { "0": "aa", "1": "bb" }, 
           { "0": "bb", "1": "cc" }, 
           { "0": "cc", "1": "11" }, .... 
          ]`

Note: first and last value must be 11

this is the code I have tried

var str =$("#gettxt").val(); //my value get from user input`

console.log(str );
var parts = str.split(" ");
var numpro =countString(str); //i have other function count word in string that user input
tbl+="";
var listt = [];
for (i = 0; i < numpro; i++) {
    listt.push({
        0:"11",
        1:parts[i]
    })
}
console.log(listt)
1
  • What is the output of the code, which you have tried? Commented Jul 28, 2020 at 4:48

5 Answers 5

1

After splitting your string, you can add a leading and trailing 11 to the array, and then make up each object from the current and next element in the array:

// user input string
var str="aa bb cc"

// split and add leading and trailing `11`
var parts = ['11'].concat(str.split(" ")).concat('11');
var numpro = parts.length;
var listt = [];
for (i = 0; i < numpro-1; i++) {
    listt.push({
        0:parts[i],
        1:parts[i+1]
    })
}
console.log(listt)

Sign up to request clarification or add additional context in comments.

Comments

1

You could split the array unshift() 11 always to be at the beginning of the array and then use map

var str="aa bb cc"
arr=str.split(" ")
arr.unshift("11")
res=arr.map((n,i)=>({"0":n,"1":arr[i+1]||"11"}))
console.log(res)

1 Comment

You' re Welcome, conceder upvoting and accepting my answer if it helped you :)
1

You could create an array by splitting the string and adding "11" at the beginning and the end. Then create an array using Array.from() and spread a chunk of the array inside the {} to create an object with numeric indices.

const str = "aa bb cc",
      parts = ["11", ...str.split(" "), "11"],
      length = parts.length - 1,
      output = Array.from({ length }, (_, i) => ({ ...parts.slice(i, i+2) }))

console.log(output)

Comments

1

Using map and split in one line

const getArray = (str, fill = "11") =>
  [...str.split(" "), fill].map((x, i, arr) => ({
    0: arr[i - 1] ?? fill,
    1: x,
  }));

var str = "aa bb cc";
console.log(getArray(str));

Comments

0

You can use Array#map with an additional push call at the end.

let parts = str.split(' ');
let res = parts.map((x,idx)=>({0: idx === 0 ? "11" : parts[idx - 1], 1: x}));
res.push({0: parts[parts.length - 1], 1: "11"});

The additional push at the end can be avoided by appending " 11" to the string before splitting.

let parts = (str + " 11").split(' ');
let res = parts.map((x,idx)=>({0: idx === 0 ? "11" : parts[idx - 1], 1: x}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.