Javascript Split String in Array to Objects in Array - javascript

I have this Array ["2.900000F02A_1313_01","2.600000F02A_1315_03","2.900000F02A_1354_01"]
And I want to split it like this:
[
{"name":"F02A_1313_01", "Voltage":"2.900000"},
{"name":"F02A_1315_03", "Voltage":"2.600000"},
{"name":"F02A_1354_01", "Voltage":"2.900000"}
]
This is my Code that doesn't work:
for (var i in msg.strg) {
array.push(i.split(/[a-zA-Z].*/g));
}
Does somebody know how I can do this?

You could split with a group.
const
data = ["2.900000F02A_1313_01", "2.600000F02A_1315_03", "2.900000F02A_1354_01"],
result = data.map(string => {
const [Voltage, name] = string.split(/([a-z].*$)/i);
return { name, Voltage };
});
console.log(result);

You could also make the match a bit more specific, matching the digits for Voltage (\d+(?:\.\d+)?) in group 1 , and a case insensitive char a-z followed by word characters (F\w+) in group 2.
const arr = ["2.900000F02A_1313_01","2.600000F02A_1315_03","2.900000F02A_1354_01"];
const result = arr.map(s => {
const m = s.match(/^(\d+(?:\.\d+)?)([a-z]\w+)$/i);
return {name: m[2], Voltage: m[1]}
});
console.log(result);

Related

How can I tell whether or not an element inside an array is a number or a word (all elements in quotes)

Background information: I have an array
this.someArray = ["Word", "123", "456"]
Where this.someArray is dynamically written (the array elements are not hardcoded)
I need to convert all items that are numbers into numbers (yes I realise that this might not make sense, essentially this is the result I want - where the numbers don't have quotes but leave the words as they are):
["Word", 123, 456]
So the steps I've thought in terms of how to achieve this:
Find out whether each element in the array is a word or number
To achieve this I have:
isNumber(number) {
return !isNaN(parseFloat(number)) && !isNaN(number-0)
}
Use a for each loop to test whether each element is a word or number
this.someArray.forEach(element => {
this.isNumber(element)
});
Write an if statement (if the element in this.someArray is a number then remove the quotes from that element)
However I'm unsure of whether step 2 is actually the correct thing to do and I'm unsure of how to write step 3
Is there a way to accomplish this?
Further info:
This is what the dynamically generated array looks like:
This is the code:
this.someArray = this.biggerArray.map((n) => {
const data = [];
for (var key of Object.keys(n)) {
data.push(n[key].data);
}
return data;
});
I think a plain .map would be easier - check if the string is composed of all digits with a regular expression, and if so, call Number on it:
const arr = ["Word", "123", "456"];
const newArr = arr.map(
str => /^\d+$/.test(str) ? Number(str) : str
);
console.log(newArr);
^\d+$ means:
^ - start of string
\d+ - one or more digits
$ - end of string
If the numbers might contain decimals, then add an optional group for the decimal portion:
const arr = ["Word", "123", "456", '12.45'];
const newArr = arr.map(
str => /^\d+(?:\.\d+)?$/.test(str) ? Number(str) : str
);
console.log(newArr);
For the array of ['Process', '1287'], it still works as expected:
const arr = ['Process', '1287'];
const newArr = arr.map(
str => /^\d+(?:\.\d+)?$/.test(str) ? Number(str) : str
);
console.log(newArr);
This approach also works for decimals within quotes.
for (let i in someArray) {
if (parseFloat(someArray[i])) {
someArray[i] = parseFloat(someArray[i]);
}
}
This is a shorter way of doing it.
for (let i in someArray) {
parseFloat(someArray[i]) && (someArray[i] = parseFloat(someArray[i]));
}

Using trim more than once in an array JS

I have a snippet of code where I am trying to parse a longer string with special characters into an array with no spaces or special characters.
input: name: this is some stuff, name2: this is more stuff
desired output: [name,this is some stuff,name2,this is more stuff]
current output: z.trim isn't a function
function parseOrder(custOrder) {
const custOrderArr = custOrder.split(',');
const trimedArr = custOrderArr.map((x) => x.trim());
const numberArr = trimedArr.map((y) => y.split(':'));
const processArr = numberArr.map((z) => z.trim());
console.log(processArr);
}
Why does trim work the first time and not the second?
You can not trim an array. But you could map the array and trim the values.
This result features Array#flatMap for preventing arrays with pairs.
function parseOrder(custOrder) {
return custOrder
.split(',')
.flatMap(y => y.split(':').map(x => x.trim()));
}
var input = 'name: this is some stuff, name2: this is more stuff ';
console.log(parseOrder(input));
Try to split by two signs, then trim your elements:
const result = str.split(/[\:,]+/).map(s => s.trim());
An example:
let str = 'test: It is me, test2: it is me 2 ';
console.log(str.split(/[\:,]+/).map(s => s.trim()));

Camelcase string to normal string

How to change NoOfSAP => No Of SAP? I have trying the replace method but it says undefined.
function unCamlelCase(result) {
return result.key.replace(/([^A-Z]*)([A-Z]*)([A-Z])([^A-Z]*)/g, '$1 $2 $3$4')
.replace(/ +/g, ' ');
};
How can I change the result key values camel case to normal string?
if (exactMatch) {
const { ...response } = json[0];
const result = Object.keys(response).reduce((acc, key) => {let newKey = key.charAt(0).toUpperCase() + key.slice(1);
return acc;
}, {});
You could use the following expression with a few helper methods to clean up the output:
"NoOfSAP".split(/([A-Z][a-z]+)/).filter(Boolean).join(' ');
This will match all upper case letters followed by one or more lower-case letters and split each chunk into an array. .filter(Boolean) is then used to remove any empty strings in the array and .join is then used to add spaces between the strings in the array.
See example below:
const getWords = wrd =>
wrd.split(/([A-Z][a-z]+)/).filter(Boolean).join(' ');
console.log(getWords("NoOfSAP")); // No Of SAP
console.log(getWords("ThisIsAWord")); // This Is A Word (notice how it will split individual letters such as A)
console.log(getWords("IAmAHuman")); // I Am A Human
console.log(getWords("JSIsGreat")); // JS Is Great (notice how it understands JS and Is are two seperate words and doesn't give JSI s Great)
As per your question about changing the keys in your object to the "uncamelcased" keys you can use .map with Object.keys to generate your result:
const getWords = wrd =>
wrd.split(/([A-Z][a-z]+)/).filter(Boolean).join(' ');
const obj = {
"NoOfSAP": 1,
"NoOfBUN": 2,
"NoOfBRE": 3,
"NoOfPEA": 4
}
const result = Object.keys(obj).map(getWords);
console.log(result);
You can go that way:
const camelToWords = (camelCaseWord) => camelCaseWord
.replace(/([A-Z]+)/g, " $1")
.replace(/([A-Z][a-z])/g, "$1");
There is also possibility to use existing libraries like lodash:
const _ = require('lodash');
console.log(_.startCase('abcDef'));
// result: Abc Def

How to check if 2 strings separated by delimiters have matching word

Trying to check if 2 strings have matching word return true.
let 1st_string = chin, kore, span;
let 2nd_string = chin eng kore zulu
1st_string.split(',').indexOf(2nd_string) > -1
I tried above code but always returns false. I need to return true as 2_nd string contains 2 matching words from 1st_string.
Solved the names and values of the variables you can do the following
let first_string = 'chin, kore, span';
let second_string = 'chin eng kore zulu';
const array1 = first_string.split(',').map(string => string.trim());
const array2 = second_string.split(' ');
function exist(list1, list2) {
for (const element of list1) {
if (list2.includes(element)) {
return true;
}
}
return false;
}
const result = exist(array1, array2);
console.log(result);
1st_string is not a valid variable name
split the first string and use Array.some() to see if the second string has any of the words in the resulting array :
let string_1 = 'chin, kore, span';
let string_2 = 'chin eng kore zulu';
const check = (str1, str2) => {
return str1.split(',').some(word => str2.includes(word));
}
console.log(check(string_1, string_2))
I think your second string will also contain a comma in between the words if yes then it is easy to achieve.
you can split the string 1 and 2 with a comma as delimiter like this
let firstString = 1st_string.split(',');
let secondString = 2nd_string.split(',');
after doing you will get the firstString and secondString variable as array then you can iterate the first array and check for duplicate using includes methods
for (let i in firstString) {
if(secondString.includes(firstString[i])){
//you can do whatever you want after finding duplicate here;
}
}

In javascript, Find an item in an array that contains a string and then return that item

I have an array of strings.
I want to search in that array for and string that contains a specific string.
If it's found, return that string WITHOUT the bit of the string we looked for.
So, the array has three words. "Strawbery", "Lime", "Word:Word Word"
I want to search in that array and find the full string that has "Word:" in it and return "Word Word"
So far I've tried several different solutions to no avail. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes looks promising, but I'm lost. Any suggestions?
Here's what I've been trying so far:
var arrayName = ['Strawberry', 'Lime', 'Word: Word Word']
function arrayContains('Word:', arrayName) {
return (arrayName.indexOf('Word:') > -1);
}
You can use find to search the array. And use replace to remove the word.
This code will return the value of the first element only.
let arr = ["Strawbery", "Lime", "Word:Word Word"];
let search = "Word:";
let result = (arr.find(e => e.includes(search)) || "").replace(search, '');
console.log(result);
If there are multiple search results, you can use filter and map
let arr = ["Strawbery", "Word:Lime", "Word:Word Word"];
let search = "Word:";
let result = arr.filter(e => e.includes(search)).map(e => e.replace(search, ''));
console.log( result );
Use .map:
words_array = ["Strawbery", "Lime", "Word:Word Word"]
function get_word(words_array, search_word) {
res = '';
words_array.map(function(word) {
if(word.includes(search_word)) {
res = word.replace(search_word, '')
}
})
return(res)
}
Usage:
res = get_word(words_array, 'Word:')
alert(res) // returns "Word Word"

Categories