Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an array in javascript. I need to remove an item from it. I have to iterate over the array and check whether there is a value called 'mastercheck'. If the value is there in array, I have to remove it and get the remaining items. How to do?
Typically my array consists of value like mastercheck,60154,60155....
First use the indexOf method to determine the index of the item with the needed value. Then you can use the splice method to remove the item at found index.
Something like that:
var array = ['mastercheck', '60154', '60155'];
var index = array.indexOf('mastercheck'); // get the index
array.splice(index, 1); // remove the item
var arr = ['mastercheck',60154,60155];
for(var i=0;i<arr.length;i++){
if(arr[i] === 'mastercheck'){
arr.splice(i,1);
}
}
console.log(arr);
Use this code jsFiddle
var arr = ['mastercheck', '60154', '60155'];
var index = arr.indexOf('mastercheck');
arr.splice(index, 1);
Related
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
I have a scenario where I have an INPUT, The User enters some text, like for ex: Cat.
I need to search an Array and check if the input from the User is present in my Array.
If it is present I ould want to remove that item from the Array and return a new Array with that item removed.
Suppose I have an arraylist like,
let arrayList = ["cat","mouse","dog"];
User INPUT = cat;
I want to check if the cat is present in arrayList , If yes then I want to remove the cat from the arrayList and want to have a new array that does not contain cat in it. Hence any INPUT provided by User needs to be removed from an array and return me a new array without that item.
You can use filter function to filter the array. Reference link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
function filterArray(arr, searchString) {
var result = arr.filter(function(e) {
return e != searchString;
});
return result;
}
filterArray(arrayList, "cat") // This will return ["mouse", "dog"];
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
I want to populate a nested array.
The first array option_array is created like this
var option_array =[];
if(!option_array.includes(row.option.option_id))
{
option_array .push({
option_id:row.option.option_id,
option_name:row.option.option_name,
max:row.option.max,
min:row.option.min,
value:row.option.value,
price:row.option.price
})
}
And now, I am creating another array option_group
var option_group=[];
if(!option_group.includes(row.option_group_id))
{
option_group.push({
option_group_id:row.option_group_id,
option_group_name:row.option_group_name,
max:row.max,
min:row.min,
option:option_array
})
}
And I want to modify option_group whereas it will add only option:option_array where current row.option_group_id is equal to the option_group.option_group.option_group_id
.includes checks if the array includes a certain item. Since you have an array of objects, you can't check if the array includes row.option_group_id. You can use find instead. Get the object with row.option_group_id from the array. If it exists, update it. Else, add a new object to the array
const found = option_group.find(a => a.option_group_id == row.option_group_id)
if (found) {
found.option = row.option_array; // update the found object
} else {
option_group.push({
option_group_id: row.option_group_id,
option_group_name: row.option_group_name,
....
})
}
I'm working with a two dimensional array to store questions and their answers. The problem I'm having is in the way I should push values into it
Cannot read property 'push' of undefined
var answers = [];
answers[0].push({"value":true, "answer":"4"});
answers[0].push({"value":false, "answer":"3"});
answers[0].push({"value":false, "answer":"2"});
answers[0].push({"value":false, "answer":"1"});
How should I link those answers (value and answer text) to specific index that represent the question number?
Currently your 0 indexed item is undefined. First initialize your 0 indexed item to refer to an array, then call push on it
var answers = []; // or [ [] ] and omit the next line;
answers[0] = [];
answers[0].push({"value":true, "answer":"4"});
answers[0].push({"value":false, "answer":"3"});
answers[0].push({"value":false, "answer":"2"});
answers[0].push({"value":false, "answer":"1"});
The way you have it setup, answers is only a 1 dimensional array.
However, if you want to store both questions and answers, I think a 1 dimensional array will suffice:
var answers = [];
answer.push(
{
question: "question string",
answer: "answer string"
});
In this specific example you should be able to get to the question with
answers[0].question
or answer with
answers[0].answer
hope it helps! :D
You don’t have elements and answers[0] is null. Try adding
answers.push([])
before pushing the others
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
I have this array [ABC, QWE, XYZ]
I would like to turn it into ['ABC', 'QWE', 'XYZ']
When I try to manipulate values in the current array I get: ReferenceError: ABC is not defined
Any ideas on how should I do it?
Thanks!
Convert arrays element types:
Number to strings
var strArr = [1,2,3,4,5].map(String);
// Result: ["1","2","3","4","5"]
We can't do that directly but after little bit change you can do that...
So the current array you said like array [ABC, QWE, XYZ],
Lets design you keys in object first:
var obj = {
ABC:1, QWE:'somevalue', XYZ:new Date()
}
So I created object obj having your variables lets say the three variables, now lets convert:
var arr = [];
for (var key in obj){
console.log(key, obj[key]);
arr.push(String(key));
}
console.log(arr);// you will see the desire result.
Running example here : example
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 8 years ago.
Improve this question
JavaScript problem. Can this be done?
I have an input array containing anything between 2 - 5 strings, each with a semi-colon delimited label to identify it. I need to de-duplicate such that the output removes the duplicates but also maintains the string identifiers, grouping if necessary.
Input Array (3 elements)
string1;apple|string2;orange|string3;orange
Output Array (now 2 elements since 'orange' appeared twice)
string1;apple|string2/string3;orange
I don't mind helping people that are just starting with a new programming language or programming: (also a js fiddle)
var arr=["string1;apple","string2;orange","string3;orange"];
var finalArr= [];
var output = {};
for(var i in arr){
var keyVal = arr[i].split(";");
if(output[keyVal[1]]==undefined){
output[keyVal[1]] = [keyVal[0]]
} else {
//should be an array
output[keyVal[1]].push(keyVal[0]);
}
}
for( var i in output){
finalArr.push(output[i].join("/")+";"+i);
}
console.log(finalArr);
I think your best option for this would be to find a way to logically group this information.
Convert the pipe-delimited string into an array.
Iterate through the array
Assign each id/value pair to a property=value pair in a struct.
Strip out the id and delimiter so you're left with the string itself in the array.
Sort the array.
Deduplicate the array.
Iterate through the array.
Iterate through the struct to generate a list of properties which values match the entry.
Unset the properties which values match the entry to reduce time in future iterations.
This is only one way of doing it. I've given you some hints on how you can approach the problem, but it's up to you to code this.