var str="Iam a fullstack javascript developer";
var strCharArr;
[...strCharArr]=str;
var arr=strCharArr.reduce((acc, cv)=>{if(cv==" ") acc.push(""); else acc[acc.length-1]+=cv; return acc;},[""]);
console.log(arr);
[...strCharArr]=str splits the string into an array of characters.
reduce starts with an array of one element of empty string ([""]),
and either adds characters, or, in case of a space, adds an empty string element.
var str="Iam a fullstack javascript developer";
var strCharArr;
[...strCharArr]=str;
var arr=strCharArr.reduce((acc, cv)=>{if(cv==" ") acc.push(""); else acc[acc.length-1]+=cv; return acc;},[""]);
console.log(arr);
[...strCharArr]=str splits the string into an array of characters.
reduce starts with an array of one element of empty string ([""]),
and either adds characters, or, in case of a space, adds an empty string element.
Here is the solution
function stringToArray(str) {
let arr = [''];
let j = 0;
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) == " ") {
j++;
arr.push('');
} else {
arr[j] += str.charAt(i);
}
}
return arr;
}
const arr = stringToArray("Iam a fullstack javascript developer")
console.log(arr[0]) // Iam
Videos
const temp = 'text';
console.log(new Array(temp));
console.log([temp]);
console.log(temp.split());
console.log([].concat(temp));
There are a few options out there.
If you want an array with the string as a single value just create a new array with that string.
temp = [temp];
String manipulation should be the last resort.
[{'Id':'01t6700000Cwxot','Name':'Product1'}, {'Id':'01t6700000CwxjY','Name':'Product2'}]
If the desired output is this one, the task would be a lot easier if you define an array then push several object.
You need an array (could be selSpot) and several object with two properties: Id and Name. You can build those object each time a user select the spot:
selSpot = [];
yourMethod() {
// something
// Define the new object
const clickedSpot = {
Id: this.record.prod_id__c,
Name: this.record.prod_name__c
};
this.selSpot.push(clickedSpot);
// pass the array to the parent
const selectedSpotEvent = new CustomEvent('meaningfulEventName', { detail: { selectedSpot: this.selSpot.map((elem) => ({...elem})) });
this.dispatchEvent(selectedSpotEvent);
}
Keep in mind that Javascript is case-sensitive, so id and Id are not the same.
Note that I deep cloned the array before passing it to the parent, this way the parent component cannot mutate the one of the child, nor its elements.
Documentation (emphasis mine)
JavaScript passes all data types by reference except for primitives.
If a component includes an object in itsdetailproperty, any listener can mutate that object without the component’s knowledge. This is a bad thing!
It’s a best practice either to send only primitives, or to copy data to a new object before adding it to thedetailproperty.
Copying the data to a new object ensures that you’re sending only the data you want, and that the receiver can’t mutate your data.
Please use meaningful names for variables, calling an array testmap may confuse others
Instead of using a string, you can populate a list directly using push. For example:
this.clickedAddSpot = [];
this.clickedAddSpot.push("{'id':'"+this.record.prod_id__c+"','Name':'"+this.record.prod_name__c+"'}");
So im trying to convert an array that was turned into a string from DynoDB. I got it formatted properly but I cant get it to work. (Incase you are wondering what this is for, its for a log keeping track of changes to a db. This is just showing the string with its state)
Here is the string:
"{PK:PHOTO01H70M6DC9YW6KPJBF15DQKDBF, SK:PHOTO01H70M6DC9YW6KPJBF15DQKDBF, type:photo, date:2023-08-02, dateRangeStart:null, dateRangeEnd:null, year:2023, featured:null, title:Title test, description:Adfsdf, linkAddress:null, location:Alberta, tags:[], people:[Free Press, School Division], bucket:bucket, region:null, key:null, filename:Screenshot 2023-07-19 at 13-44-34 Admin Add Files.png, filedesc:null, dateSK:2023-08-02#dodeyby, uploadedBy:deathbyunknown, createdAt:2023-08-04T15:55:54.644Z, updatedAt:2023-08-04T15:58:20.579Z}"
Things I've tried:
Convert using Object.entrys(), Result is an array with all the characters as a single item in the array.
JSON.parse(), Result error message: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data.
EDIT: Santizied Information that shouldn't be shown