Working with associate array in javascript - javascript

I making one application which contains 2 input box and one input button.My qestion is when user enter some input eg. "I used to going somewhere" then my result should be "I UD GNG somewhere." For that i am using this code http://pastebin.com/vHkhASdZ
Please,anyone have idea how to solve then pls reply me asap. I solved my issue in php but in case of javascript i don't have any idea. Here is my output link in php http://codepad.viper-7.com/SQvO6Y
I want my result in javascript.Pls anyone know then give reply.

Use this function
var replaceText = function () {
var inputval = document.getElementById('first_text').value;
var arr = {
"going": "GNG",
"used to": "UD",
"as soon as possible": "ASAP",
"to do fast": "tdf"
}
for(var key in arr) {
if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
}
document.getElementById("second_text").value = inputval;
}

Do something like this. Define word replace as map.
var wordmap = {
going: 'GNG',
'used to': 'UD',
'as soon as possible': 'asap',
'to do fast': 'tdf',
..
}
and define function that iterates every word in the input string.
function replace( text ) {
var input = text.split(' ');
var outputval = []
for (var i in input) {
outputval.push( wordmap[input[i]] || input[i] )
}
return outputval.join(' ');
}
And use it like this
var output = replace( document.getElementById('first_text').value );

Related

Split Data for certain condition In JavaScript Pentaho

I have a column in csv file named event_name and I am using Modified Java Script Value step to create new columns if the first character of event_name fufill conditions , for example :
event_name ="app_connection: connection_start_time = 10/05/2018 17:29:26: connection_end_time = 10/05/2018 17:29:29: connection_duration_in_seconds = 3"
==> event = app_connection
==> connection_start_time = 10/05/2018 17:29:26
==> connection_end_time = 10/05/2018 17:29:29
==> connection_duration_in_seconds = 3
if it's : event_name ="scene_access"
==> event = scene_access
I tried this code but it doesn't seem to change anything :
if(event_name.substr(0,3).equals("app"))
{
var event = event_name.substring(0,14);
var connection_start_time = event_name.substr(40,59);
var connection_end_time = event_name.substr(83,102);
var connection_duration_in_seconds = event_name.substr(137,139);
}
else
{
event_name = event_name;
}
If you could give me a hint or explaining what I'm missing will be a huge help.
Thank you.
I tried the field in the substring function, and in the substr function the sintaxis should be substr(string, position_from, number_of_characters).
With this change the code should be:
if(substr(event_name,0,3).equals("app"))
{
var event = substr(event_name,0,14);
var connection_start_time = substr(event_name,40,19);
var connection_end_time = substr(event_name,83,19);
var connection_duration_in_seconds = substr(event_name,137,1);
}
else
{
event_name = event_name;
}
After this press the button "Get variables" to make available the fields in the output of the step.

How to add an item to a list by replacing the earlier one. In Javascript

I have a list will a list but it only has one item I need to be able to replace that with a new one. But there should one be one.
Because there is a condition involve I need to be able to create an variable if something there or not but I found the list to be easier so how can I replace an item in a list with a new one
let subject_reply = [];
if (email.subject.startsWith("Re:") == true) {
subject_reply.push(email.subject);
}else{
subject_reply.push(`Re: ${email.subject}`);
}
Maybe this helps you, to replace an item of an array you can use splice method, this code says: replace item at index 1, delete that item and insert 'Boxer'.
var dogs = ['Bulldog', 'Beagle', 'Labrador'];
dogs.splice(1, 1, 'Boxer');
console.log(dogs);
I'm not sure if that is what you asked for, it's an array that holds one item only, by clearing the array when we click on the button and it takes the value of the input and then pushing it to the array by making it the only value.
If the `value` starts with *Re:*{
Push as it is.
} else {
Push: "Re: value"
}
var myInput = document.getElementById('myInput');
var myBtn = document.getElementById('myBtn');
let subject_reply = [];
myBtn.addEventListener('click', () => {
subject_reply = [];
var textValue = myInput.value;
if (textValue.startsWith("Re:") == true) {
subject_reply.push(textValue);
} else{
subject_reply.push(`Re: ${textValue}`);
}
console.log(subject_reply);
});
<input type='text' id='myInput'>
<button id='myBtn'>Click</button>
It's difficult to answer a poorly-worded question. As I understand it, one of these is the answer you seek:
subject_reply.push("Re: " + email.subject.replace(/^Re:\s?/, ""));
OR
subject_reply[0] = "Re: " + email.subject.replace(/^Re:\s?/, "");
I understand you want to replace a string value in an array.
For this, Function "Replace()" will help. In this problem specific to you, "EmailREAdder()" will help you adding "RE:" at the start of a string which does not contain "RE:".
/* Function for replacing an item */
const Replace = (arr,oldValue, newValue) => arr.splice(arr.indexOf(oldValue), 1, newValue);
/* Setting all without "RE:" to RE*/
const EmailREAdder = arr => {
arr.map(item => {
if(!item.match(/RE:/)){
Replace(arr, item, `RE: ${item}`);
}
});
}
/* code */
let users = [
"RE: Farman Ali",
"Usman",
"Fraz"
];
console.log(users);
EmailREAdder(users);
console.log(users);

Node js merge values basic implemenation

I am trying to do something like the below but merge function has a problem with the line
content = content.replace( content, "Hi" + values.first_name + "! Thanks for completing this code challenge :)" );
Filename : app.js
var utilities = require("./utilities");
var mailValues = {};
mailValues.first_name = "Janet";
var emailTemplate = "Hi %first_name%! Thanks for completing this code challenge :)";
var mergedContent = utilities.merge(emailTemplate, mailValues);
//mergedContent === "Hi Janet! Thanks for completing this code challenge :)";
Filename : utilities.js
function merge(content, values) {
content = content.replace( content, "Hi" + values.first_name + "! Thanks for completing this code challenge :)" );
return content;
}
module.exports.merge = merge;
Your merge function is not good, it does a weird thing and will surely returns a weird string if you pass another template. You're replacing the entire input string by the "Hi...." string but with first_name inserted so in the end, your function is too specific and can't handle extra parameters or another template/string.
function merge(content, values) {
// loop over all the keys in the values object
Object.keys(values).forEach(function(key) {
// look for the key surrounded by % in the string
// and replace it by the value from values
content = content.replace('%' + key + '%', values[key]);
});
return content;
}
var mailValues = {};
mailValues.first_name = "Janet";
mailValues.last_name = "Doe";
var emailTemplate = "Hi %first_name% %last_name%! Thanks for completing this code challenge :)";
var mergedContent = merge(emailTemplate, mailValues);
document.write(mergedContent);
Try the "run code snippet" button.
for ... in version, for information, better to use the previous version.
function merge(content, values) {
// loop over all the keys in the values object
for (var key in values) {
// look for the key surrounded by % in the string
// and replace it by the value from values
content = content.replace('%' + key + '%', values[key]);
}
return content;
}
var mailValues = {};
mailValues.first_name = "Janet";
mailValues.last_name = "Doe";
var emailTemplate = "Hi %first_name% %last_name%! Thanks for completing this code challenge :)";
var mergedContent = merge(emailTemplate, mailValues);
document.write(mergedContent);

Find any variable that is undefined and hide from url string

I'm currently building a URL string from form inputs to look something similar to this:
api/search/127879/11-28-2013/7/2/undefined/undefined/undefined/LGW/null
How would i find any or all variables that === to undefined and then remove from these from the string above?
if you dont want to chage logic of building url than just make use of replace function of javascript
var str = "api/search/127879/11-28-2013/7/2/undefined/undefined/undefined/LGW/null";
var res = str.replace(/undefined\//gi,"");
If you are generating string yourself, then check before appending it to urlString.
If you get the string from server, then do this:
var finalUrl = "";
var urlStr= "api/search/127879/11-28-2013/7/2/undefined/undefined/undefined/LGW/null";
var urlArray = urlStr.split('/');
for (i = 0, i = urlArray.length; i ++) {
if(urlArray[i] === undefined)
{
//do nothing
}
else
{
finalUrl += urlArray[i];
}
}
Hope this helps.

don't get return associative array in javascript

When i create associate array in javascript, i got a problem like that.
I want to get the value by using field name as key, but i just only got undefined.
What should i do to get value by key or which way is good approach for it.
Here is my code
function getFields(pVal){
var tmpObj = {};
str = pVal.split(",");
for(i=0;i<str.length;i++){
tmpVal = str[i].split(":");
tmpObj[tmpVal[0]] = tmpVal[1];
}
return tmpObj;
}
function JustTest(){
var fields = {};
fields = getFields("'Code':'PRJ001','Name':'Project 01'");
alert(fields['Code']);
}
Because the key is 'Code', not Code, note the single quote ', you need do alert(fields["'Code'"]);
PS: Please add ; at the end of statement, it is bad practice to omit them.
I have re-factor the code, just try this:
function getFields(pVal) {
var tmpObj = {};
var str = pVal.split(",");
for (var i = 0; i < str.length; i++) {
var tmpVal = str[i].split(":");
tmpObj[tmpVal[0]] = tmpVal[1];
}
return tmpObj;
}
function JustTest() {
var fields = { };
fields = getFields("'Code':'PRJ001','Name':'Project 01'");
alert(fields["'Code'"]);
}
if you have question please comment below about code, thanks

Categories

Resources