Javascript convert [[ ]] to [] [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Convert
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']]
to
var a= ['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']
I dont want two square brackets in front and end.
I want output
a[2] = 'a123'

The best way to do is just assigning the first value to the array:
var a = [['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']];
a = a[0];
console.log(a);
But the right way of dealing it should be making sure that the endpoint or whatever that outputs should output correctly.
You can use Array.prototype.flat().
console.log([['12ae11ee12-1bhb222','2019-10-10T19:46.19.632z','a123']].flat());
This solution works if it's extremely nested too:
console.log([
['12ae11ee12-1bhb222'],
['2019-10-10T19:46.19.632z', 'a123']
].flat());

Related

How to parse object array that has been stringified using backslashes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I have been searching for the correct regex for converting this string to JSON. It works if there is a single object in the array, but I believe the comma is messing it up somehow.
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
const obj = JSON.parse(json.replace(/("[^"]*"\s*:\s*)(\d{17,})/g, '$1"$2"'));
console.log(obj);
There was nothing wrong except that the list of objects was not contained within square brackets
const json = "{\"userId\":44, \"userName\": \"Jim Coleman\"},{\"userId\":33515, \"userName\": \"Grace Mamaradlo\"}";
console.log(JSON.parse(`[${json}]`))

How to iterate and append characters of an array by push [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
var charArray=[a,b,c]
I want to have a new array as following:
var charArrayNew=[a, ab, abc]
Please suggest how to get the result of charArrayNew in ES6 Javascript.
You could do this:
const charArray=['a','b','c'];
const answer = charArray.map((_,i,ar)=>ar.slice(0,i+1).join(""));
console.log(answer);
// or, alternatively:
fn=(ar,res=[])=>(
ar.reduce((a,c)=>(res.push(a+=c),a),""),res );
console.log(fn(charArray))

REMOVE SPECIFIC NUMBER WITH JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'M TRYING TO HANDLE THIS DATA IN JAVASCRIPT SO HOW CAN I REMOVE DATA ACCORDING TO THE ID NUMBER THE FIRST NUMBER
600030/01/2018/163904
600010/01/2014/3789
600030/01/2020/47104
600030/01/2012/39104
600010/01/2011/93817
how can i remove all 600030 data so i get
600010/01/2014/3789
600010/01/2011/93817
If all data shaped in Array and element is instance of String.
I would recommend Array.prototype.filter and String.prototype.startsWith();
let data = [
'600030/01/2018/163904',
'600010/01/2014/3789',
'600030/01/2020/47104',
'600030/01/2012/39104',
'600010/01/2011/93817',
];
let filteredData = data.filter((d)=>!d.startsWith('600030'));
console.log( filteredData );
Result is
[
'600010/01/2014/3789',
'600010/01/2011/93817'
]

Remove non-numeric items in a listu using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)

Horizontal concatenation of two array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
how to do horizontal concatenation of two arrays in javascript?
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
I want output like :
c = [[1,2,7], [3,4,8], [5,6,9]]
You can use map and use index to access corresponding value from second array
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
let final = a.map((v,i)=> v.concat(b[i]))
console.log(final)

Categories

Resources