2

I have an object (key value pair) looks like this

enter image description here

I want to get a string of '[100000025]/[100000013]'

I can't use var str = OBJ[0].PC + OBJ[1].PC (which gives me '100000025100000013') because I need the bracket structure. The number of items can vary.

Added >> Can it be done without using arrow function?

5 Answers 5

6
 const string = array.map(({PC}) => `[${PC}]`).join('/')

You could map every string to the string wrapped in brackets, then join that by slashes.

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

1 Comment

Great! Thanks for the answer! Can it be done without using arrow function?
2

You can use a map() and a join() to get that structure. - this is hte same solution as Puwka's = but without the template literal.

var data = [
  {am: 1, ct: "", pc: "1000000025"},
  {am: 2, ct: "", pc: "1000000013"}
];

let newArr = data.map(item => "[" + item.pc +"]");

console.log(newArr.join("/")); // gives [1000000025]/[1000000013]

Comments

1

You can always use classic for in loop

let arr = [{PC:'1000'},{PC:'10000'}]
let arrOut = [];
for(let i = 0; i < arr.length; i++) {
    arrOut.push('[' + arr[i].PC + ']'); 
}

now the arrOut is equal ["[1000]", "[10000]"] what we need is to convert it to a string and add '/' between items.

let str = arrOut.join('/');
console.log(str) // "[1000]/[10000]"

2 Comments

Great! this is what I've been wanting for! Thanks!
OP, please update and select the right answer for future developers to help with. thank you;
1

So you need a string in the format of: xxxx/yyyyy from a complex object array.

const basedata = [...];
const result = basedata.map( item => `[${item.PC}]` ).join('/')

so i will explain it now. The map function will return a new array with 1 entry per item. I state that I want PC, but i added some flavor using ticks to inject it inbetween some brackets. At this point it looks like: ["[1000000025]","[100000013]"] and then join will join the arrays on a slash, so it will turn into an array.

"[100000025]/[100000013]"

Now, this will expand based on the items in your basedata. So if you have 3 items in your basedata array, it would return:

"[10000000025]/[100000013]/[10000888]"

Comments

0

First if you want to divide the result then it will be better to change it into number and then just do the division. Example

Number.parseInt("100000025")/Number.parseInt("100000013")

If you want to display it then better to use string interpolation surround it with back tick [${[0].PC}]/[${[1].PC}]

Hope this is what are you looking for

1 Comment

I think OP wants the result to be the string [100000025]/[100000013] not, the mathematical operation100000025 / 100000013 of dividing two numbers.

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.