|
I m new to javascript and I want to filter specific elements into new array. Eg ['a', 'b', 'c', 'd', 'e', 'f'] to [' c', 'd', 'f']
|
|
|
|
|
If you are new to a language then it is always a good idea to work through one of the many online tutorials, and get familiar with the basic features of the language. You can read all about arrays at JavaScript Arrays[^].
|
|
|
|
|
var input = ['a', 'b', 'c', 'd', 'e', 'f'];
var filter = ['a', 'b', 'e'];
var output =[];
for (var i = 0; i < input.length; i++) {
if (filter.indexOf(input[i]) == -1)
output.push(input[i]);
}
Customise it depends on your need
|
|
|
|
|
Hi ,
I need to remove duplicate entries.
ex : Here "Voltage" array is repeating, having one is enough.
Please help anyone to solve this.
<pre lang="Javascript">var data = [
{
"metadata" : {
"names":["times","values","types"],
"types":["time","linear","ordinal"]
},
"data": [
["0",2.37130,"Global"],
["1",2.37130,"Voltage"],
["1",2.37130,"Voltage"],
["2",10.30980,"Intensity"],
],
}
];</pre>
|
|
|
|
|
Here's some logic which, although not the most elegant will work across many programming languages. It is to teach you to solve a problem by breaking it down to smaller levels. Note that there are languages the let you automatically compare two arrays with a single function and this becomes much easier.
First, sort your array: any simply sort will do because if any entry is a mismatch then the array's your comparing are different. Sort by element 0, 1, 2, would be simplest to work with.
Second, for a 2-dimensional array, you can use nested loops.
Use a for() loop to operate on array elements explicitly by index so you can compare n and n+1
Similarly, work on the internal elements of of array n vs n+1, comparing them.
If you reach a failure to match, do a continue to the outer loop.
If you reach the end, they're a matched pair. Store the index of one of them.
Return to the outer loop for next (n+1, n+2).
When done, you can remove the duplicate members from the stored index list.
Various languages will allow you to do shortcuts.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
you can use Underscore.js. So you do not need to code from scratch
|
|
|
|
|
I think my query is right I am having trouble adding Quantity and TotalPrice to the output
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var record = new sforce.SObject("Opportunity");
record.Id = '{!Opportunity.Id}';
var retriveOpptyLineItems = sforce.connection.query("Select PricebookEntry.Product2.Name, Quantity, TotalPrice From OpportunityLineItem WHERE OpportunityId = '{!Opportunity.Id}' and (NOT Name like '%Discount%')");
var strProductNames = '';
for(var i=0; i<retriveOpptyLineItems.records.length ; i++){
strProductNames += retriveOpptyLineItems.records[i].PricebookEntry.Product2.Name + ',' + '\n ';
}
if(strProductNames.length>0){
strProductNames = strProductNames.substring(0,strProductNames.length-1);
}
record.Sample_Product_Name_1__c = strProductNames;
sforce.connection.update([record]);
window.location.reload();
|
|
|
|
|
I've never worked with SafesForce, but I'm going to guess that your query returns a promise, and that you're attempting to act on the promise object rather than its resolved value.
Never mind, it looks like that's supposed to be synchronous. Sorry, too busy giggling into my coffee to find an answer.
Just FYI, your code is completely vulnerable to SQL injection.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
This throws an unexpected identifier error that has me baffled
Thanks
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")}
var record = new sforce.SObject("Opportunity");
record.Id = '{!Opportunity.Id}';
qOli = Select Amount,CloseDate,Name,
(Select PricebookEntry.Product2Id,
TotalPrice,
UnitPrice,
ListPrice
From OpportunityLineItems)
From Opportunity;
record.Sample_Product_Name_1__c= qOli;
window.location.reload();
|
|
|
|
|
What language is that supposed to be? It doesn't look like valid Javascript to me.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi i have table have 5 comnun
Name Salary Bonus Deduction Total
Ahmed 500 500 100 900
calculation of total is
Total=Salary+Bonus-Deduction
and total in red color according to my code
what i need actually if i changed in Salary cell or Bonus cell or Deduction cell affect in total cell
suppose i added row above then edit salary
from 500 to 2000 meaning in this time row will be as bellow
Ahmed 2000 500 100 2400
total will be 2400 with green color
i can do by button but how to do by changing cell in table affect in total
@{
Layout = null;
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#btn").click(function () {
var x = $("#txt1").val();
var y = $("#txt2").val();
var z = $("#txt3").val();
var M = $("#txt4").val();
var L = parseInt(y) + parseInt(z) - parseInt(M);
$("#tb").append(" " + x + " " + y + " " + z + "" + M + "" + L + "");
$("#tb tr").each(function () {
var total = $(this).find("td:nth-child(5)").html();
if(parseInt(total)>1000)
{
$(this).find("td:nth-child(5)").css("background-color", "green");
}
if (parseInt(total) < 1000) {
$(this).find("td:nth-child(5)").css("background-color", "red");
}
if (parseInt(total) == 1000) {
$(this).find("td:nth-child(5)").css("background-color", "yellow");
}
});
});
$("#tb").on("click", "tr", function () {
$(this).find("td").slice(0, 4).prop("contenteditable", true);
});
});
</script>
.red{
color:#ff0000;
font-weight:bold;
}
</head>
<body>
Name<input type="text" id="txt1" />
Salary<input type="text" id="txt2" />
Bonus<input type="text" id="txt3" />
Deduction<input type="text" id="txt4" />
<input type="button" value="add" id="btn" />
Name
|
Salary
|
Bonus
|
Deduction
| total
|
</body>
</html>
|
|
|
|
|
in my code below i can edit row success without any error but if i need to cancel value edited in row OR get value before changed what i write to cancel edit in row in table by using jquery my code as following
<br />
@{<br />
Layout = null;<br />
}<br />
<br />
<!DOCTYPE html><br />
<br />
<html><br />
<head><br />
<meta name="viewport" content="width=device-width" /><br />
<title>Index</title><br />
<script src="~/Scripts/jquery-1.10.2.js"></script><br />
<script><br />
<br />
$(function () {<br />
$("#btn").click(function ()<br />
{<br />
var x = $("#txt1").val();<br />
var y = $("#txt2").val();<br />
var z = $("#mycountry").val();<br />
$("#tb").append("<tr> <td>" + x + "</td> <td>" + y + "</td> <td>" + z + "</td><td> <input type='button'class='c' value='Delete'/></td><td> <input type='button' class='d' value='Edit'/></td><td><input type='button' class='e' value='Cancel'/></td></tr>");<br />
<br />
<br />
});<br />
$("#tb").on("click", ".c", function () {<br />
$(this).closest('tr').remove();<br />
});<br />
$("#tb").on("click", ".d", function () {<br />
var row = $(this).closest('tr').toggleClass("editing");<br />
row.find("td").slice(0, 2).prop("contenteditable", row.hasClass("editing"));<br />
var myselect = '<select id="mycountr1" name="mycountry1">' +<br />
'<option>---select---</option>' + '<option>Egypt</option>' + '<option>qatar</option>' + '<option>saudia</option>' + '<option>emarates</option>'<br />
'</select>';<br />
var dropcountry = $(this).parent().prev().prev().text;<br />
$(this).parent().prev().prev().html(myselect);<br />
<br />
<br />
});<br />
$("#btndis").on('click', function () {<br />
$("body").append("<ul id='listNames''></ul>");<br />
$('#tb td:nth-child(2)').each(function () {<br />
$("#listNames").append("<li>" + $(this).text() + "</li>")<br />
});<br />
});<br />
<br />
<br />
});<br />
</script><br />
<br />
<style><br />
.editing {<br />
background: yellow;<br />
}<br />
</style><br />
</head><br />
<body><br />
<div><br />
ID<input type="text" id="txt1" /><br /><br />
Name<input type="text" id="txt2" /><br /><br />
Country: <select id="mycountry"><br />
<option>---select---</option><br />
<option>Egypt</option><br />
<option>qatar</option><br />
<option>saudia</option><br />
<option>emarates</option><br />
</select><br /><br />
<input type="button" value="add" id="btn" /><br />
<input type="button" value="display" id="btndis" /><br />
<br />
<table><br />
<thead><br />
<tr><br />
<td><br />
ID<br />
</td><br />
<td><br />
Name<br />
</td><br />
<td><br />
Country<br />
</td><br />
<td><br />
</tr><br />
</thead><br />
<tbody id="tb"></tbody><br />
</table><br />
</div><br />
</body><br />
</html><br />
|
|
|
|
|
I have written this code of Tic Tac Toe using HTML and Javascript.
Since I am new to programming, can somebody please help me in identifying what is not right in it.
Project.rar - Google Drive[^]
|
|
|
|
|
If you want help then you need to explain what is wrong, and show the part of the code that is failing. Please edit your post and add full details of the problem.
|
|
|
|
|
Firstly , although if one of us go through your project and fix the code for you , to be honest you will not learn anything.
I would advise you to put some effort and understand the code and when you struggle with some errors then we can help you. This way you could learn something and understand about your project.
|
|
|
|
|
I am just wrapping up a HUGE project that took me forever to research and code. There is just one thing keeping it from working properly. An updated div on my website.
I have a div that contains css code that displays a temperature gauge. I have php code that extracts data out of a txt file for the gauge. It works but I have to manually update the whole page or update the whole page in the code. I just need that one div to update every second or so and to reload the txt file before doing so.
I have found lots of javascript/ajax examples but none are explained and none are commented. I just dont seem to understand how they work. I have no javascript experience. Does anyone have a quick example with comments or can explain what is going on in the code?
Thanks to anyone for reading.
|
|
|
|
|
is it possible to put some code snippet for us to look at before we jump on big wagons?
|
|
|
|
|
Member 12634098 wrote: or can explain what is going on in the code? No, because you haven't posted any.
However, to do something in a web page on a timed basis you can use setTimeout(). W3Schools.com does a good job of explaining and showing examples, Window setTimeout() Method[^].
So, you could do something like:
setTimeout("myFunction", 1000);
...
function myFunction(){
}
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi there, new on this forum and looking for help with some code I found at Cross Browser RDLC Printing - CodeProject. The author didn't include source or demo files and does not reply to my comments either, so I turned to this forum for help.
The author supposedly shows a tip on how to create a print button for rdlc reportviewer for non IE browsers:
...If not, we should create a client-side print icon button on the fly.
How to create one that exactly looks like the original print button?
The answer is so straightforward, by copying the markup for the original button.
Here is what RDLC uses for creating the print icon:
$print = $('<div class=" " style="display:inline-block;
font-family:Verdana;font-size:8pt;vertical-align:top;"><table title="Print">
<tbody><tr><td><input name="ReportViewer1$ctl05$ctl06$ctl00$ctl00$ctl00"
title="Print" style="width: 16px; height: 16px;" type="image"
alt="Print" src="/Reserved.ReportViewerWebControl.axd?
OpType=Resource&Version=11.0.3366.16&
Name=Microsoft.Reporting.WebForms.Icons.Print.gif"></td></tr></tbody
</table></div>');
- Notice that we created a JavaScript object which represents the markup.
This icon must be added to the RDLC viewer toolbar and this is how to find the toolbar object on the client-side:
var findtoolbar = function () {
var $all = $('table div');
for (var i = 0, len = $all.length; i < len; i++) {
if ($($all[i]).css('background-image').toLowerCase().indexOf('toolbar_bk.png') != -1)
return $($all[i]);
}
return null;
}
As you see, there is no solid identification method for the toolbar, so we used a combination of certain properties like background image along with the order of objects in the DOM...
Well he does not show WHERE and HOW to actually add the icon to the toolbar. I tried to do
$(document).ready(function () {
findtoolbar.add($print);
});
and it threw an error (of course?), but with my limited knowledge of javascript and jquery, that's all I could come up with. So I was hoping someone here could help me make some sense out of this . Thanks in advance.
The perfect woman: cooks good food and never says no in the middle of the night.
|
|
|
|
|
Jaime Premy wrote: with my limited knowledge of javascript and jquery, The code you have posted is essentially a way to hack a webpage. So, having a limited knowledge or JavaScript is going to make this tough for you, which I suppose you already know.
I suggest putting a breakpoint in the code and then seeing what it does. You're essentially trying to inject your own code into a Microsoft product and trick it into using what you have. In other words, I would be surprised if it worked all the time anyway. It is an unsupported hack.
That article is also 2 years old and browsers have changed a bit since then. Also, the report viewer control I know changes often between versions so make sure you are using version 11, which is what the article says it supports.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Well my problem at this point is really how to add the js generated button to the toolbar. This hack is necessary since ms simply did not update reportviewer for compatibility with modern browsers, not even IE.
The perfect woman: cooks good food and never says no in the middle of the night.
|
|
|
|
|
I believe this code does it,
var findtoolbar = function () {
var $all = $('table div');
for (var i = 0, len = $all.length; i < len; i++) {
if ($($all[i]).css('background-image').toLowerCase().indexOf('toolbar_bk.png') != -1)
return $($all[i]);
}
return null;
}
Like I said, this looks like a hack to inject your own code into existing MS product so there is no guarantee that it will work.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Humm..., this is the code for finding the toolbar. My problem is how to ADD the javascript generated button to the toolbar...
The perfect woman: cooks good food and never says no in the middle of the night.
|
|
|
|
|
Yes, I have understood you every time. What you are not getting is that you need to debug the code. I don't have the answer for you but that person took the time to write an article and I trust that it is correct. So, findtoolbar is code to find the toolbar but if it is in the article it's important for a reason. The $print part looks like it is the actual button. I believe, as I have stated, that findtoolbar is an existing Microsoft function that gets called already. The article, I believe, is hoping that this new findtoolbar function will get called instead of the built-in one and this new one works in chrome and firefox.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I’m learning nodeJS.
I don’t know what is the purpose of
callback argument in
module.exports . It pretty much look like
return and
console.log
here is my code
var request = require('request');
module.exports = function(location,callback) {
var encodedUrl = encodeURIComponent(location);
var url = "http://api.openweathermap.org/data/2.5/weather?q="+encodedUrl+"&APPID=575991c6b55a139d32139652d78bc8b8&units=metric";
if(!location) {
callback("No location provided");
} else {
callback('location is '+location);
}
request({
url: url,
json: true
}, function (error,response, body) {
if(error) {
callback('Unable To Fetch Weather');
} else {
callback("Current Temperature in "+body.name+" "+body.main.temp + '\xB0!!!');
}
});
};
|
|
|
|
|