This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 2 years ago.
I have an object 'myObjects':
let myObjects = {
"obj1":["X1","1.2"],
"obj2": "2",
"obj3": "3"
};
I am trying to only remove the X1 from the obj1 section. I have tried doing:
delete obj1[0];
however end up with
[null,"1.2"]
how do I get rid of the null?
You are going to remove element from Array not Object. In that case, you can use Array.splice as follows.
let myObjects = {
"obj1":["X1","1.2"],
"obj2": "2",
"obj3": "3"
};
delete myObjects.obj1.splice(0, 1);
console.log(myObjects);
Related
This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 9 months ago.
I have an object that looks like the example below. I want the object that is equal to the id in the variable to be sorted first. How can I do that?
const id = 15;
const obje = [
{
"name": "Join",
"phone": "1234",
"email": "test#mysite.com",
"id": "12"
},
{
"name": "Join2",
"phone": "4321",
"email": "test2#mysite.com",
"id": "15"
}
]
What I want to do is to rank the object equal to the id in the variable to the top.
Thanks for your help in advance 🙏🏻
Instead of sorting find the index of the object where its id matches the search id, then splice it out, and then add it to the start of the array with unshift. You are mutating the array but it's quick, and without knowing the complete structure of your array, probably the easiest approach.
const id=15,arr=[{name:"Join",phone:"1234",email:"test#mysite.com",id:"12"},{name:"Join2",phone:"4321",email:"test2#mysite.com",id:"15"}];
const index = arr.findIndex(obj => obj.id === id);
arr.unshift(arr.splice(index, 1)[0]);
console.log(arr);
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
while inspecting in browser, I am getting this type of array and I am unable to access 0th index of array.
function xyz()
{
var obj={
name: "abc",
age: "20"
var VArr=[];
VArr.push(obj);
}
[]
0:Array(1)
0:{name:"abc",age:"20"}
First acess to the array by VArr[0] inside this u have your objecet to access your object property use dot notation like this
console.log(VArr[0].name)
Full code
function xyz(){
var obj={
name: "abc",
age: "20"
}
var VArr=[];
VArr.push(obj);
console.log(VArr[0].name)
}
xyz()
This question already has answers here:
How to search JSON tree with jQuery
(11 answers)
Closed 5 years ago.
I have the following object "list":
{
...
2: {id: 35, name: 'dog Sharik'},
3: {id: 36, name: 'cat Murzik'}
...
}
Need to find object which consist 'cat' word in name.
What is the best way to do it (does Jquery can help with it?)
Iterate the loop and find it.Use a for loop to iterate the obj and return when the name property of the object contains the word cat.Here obj is your original object
CODE
function findmatching(obj,word){
for(var key in obj){
if(obj[key]['name'].indexOf(word)!=-1){
return obj[key]
}
}
}
var mymatchingvalue=findmatching(userinput,word)
You can use lodash/underscore.js
_.filter(list, function(d) { return _.includes(d.name, 'cat') })
This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
Closed 6 years ago.
I'm trying to convert a JSON key value object to an Array, but I'm not sure how to get it in the format I need. What I have for the JSON is something similar to below:
{
"01": "yes",
"02": "yes",
"03": "no"
}
but I need an Array like the one below so I can iterate through it easily:
["01:yes","02:yes","03:no"]
or is it possible to iterate through this JSON object while accessing the keys and values easily?
Use Array#reduce
Object.keys() returns an array of a given object's own enumerable properties
var obj = {
"01": "yes",
"02": "yes",
"03": "no"
};
var op = Object.keys(obj).reduce(function(a, b) {
return a.concat(b + ':' + obj[b]);
}, []);
console.log(op);
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 8 years ago.
Problem: Create a javascript function that takes an object (of any size and depth), iterates through it and runs some basic string replacing on any strings and returns the object with the amended values.
I have two ideas about the implementaion, but cannot get a solution for either:
var context = {
"test1": "123",
"test2": "123",
"test2.2": "123",
"test3": {
"test4": "cats",
"test5": {
"test6": "test1",
"test123": "1231232"
}
}
};
Idea 1)
Loop the array, and change the values,
http://php.net/manual/en/language.references.pass.php
In some way similar to PHP
Idea 2)
Build an array of path(s) to the object, so to replace the "test123" value I can create such an array:
['test3', 'test5', 'test123']
... this part is simple, but how do I then convert this to something like:
context['test3']['test5']['test123'] ?
Thankyou in advance.
Loop over the object and invoke the function recursively if the value at hand is an object. In pseudocode:
function replaceInObject ( obj, find, repl)
for key in obj
value = obj[key]
if value is object
obj[key] = replaceInObject(value, find, repl)
else
obj[key] = value.replace(find, repl)
return obj