Remove element from nested array [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
The community is reviewing whether to reopen this question as of 6 months ago.
Improve this question
I have this data:
const pages = [
[
{id: 1},
{id:2}
],
[
{id: 3},
{id:4}
],
];
What would the best and more efficient way to remove an element from this structure having 2 indexes as params?
e.g. index1 = 1 and index2 = 0 will remove pages[1][0] and pages will be:
const pages = [
[
{id: 1},
{id:2}
],
[
{id:4}
],
];

You can use array.splice to remove an item from an array:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The splice() method changes the contents of an array by removing or
replacing existing elements and/or adding new elements in place. To
access part of an array without modifying it, see slice().
This is the demo with a function implementing what did you request for:
const pages = [
[
{id: 1},
{id:2}
],
[
{id: 3},
{id:4}
],
];
removeItem(1,0);
console.log(pages);
function removeItem(index1, index2){
pages[index1].splice(index2, 1);
}

Try this :
const idx1 = 1;
const idx2 = 0;
pages[idx1].splice(idx2, 1);

Related

How to filter array value to object value using javascript? [closed]

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 4 days ago.
Improve this question
let filtered = [
[
"name",
"contains",
"men"
],
[
"web",
"contains",
"www.google.com"
],
[
"phoneNumber",
"contains",
"017XXXXXXXXXX"
]
];
Expected result:
let filtered = {
name: {
contains: "men"
},
web: {
contains: "www.google.com"
},
phoneNumber: {
contains: "017XXXXXXXXXX"
}
}
I need to filtered upper array elements to object elements using javascript. Here array value need to object key & value
let filteredObject = Object.create(null);
for(let filter of filtered) {
filteredObject[filter[0]] = Object.create(null);
filteredObject[filter[0]][filter[1]] = filter[2];
}
Object.create(null) may be omitted and replaced with {} if you are certain that a trusted entity is in control of the strings in the filtered array.

How to filter/search for multiple keys in an object? [closed]

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 11 months ago.
Improve this question
I have a dynamic array whose length keeps changing
Ex: let array = [1,2]
I have an array of objects, whose length also keeps changing:
let obj = [
{id: 1, value: abc, location: xyz},
{id: 2, value: pqr, location: xyz},
{id: 3, value: rqp, location: xyz}
]
I want to return a final array of objects which has the id of "array" in "obj" i.e
[
{id: 1, value: abc, location: xyz},
{id: 2, value: pqr, location: xyz}
]
Does filter work in this case?
You can use Array.prototype.filter and Array.prototype.includes for this.
obj.filter((item) => array.includes(item.id))

How do I group muiltiple record in array to object in Javascript? [duplicate]

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 9 years ago.
Improve this question
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.

Convert plain array to Array of objects [closed]

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 3 years ago.
Improve this question
I have an array like this
let data = ['String1', 'String2', 'String3', 'String4']
I need to convert this array to an array of objects like so:
data = [
{value: 0, label: 'String1'},
{value: 1, label: 'String2'},
{value: 2, label: 'String3'},
{value: 3, label: 'String4'}
]
How to achieve this most elegantly?
Use map():
const arr = ['String1', 'String2', 'String3', 'String4'];
const res = arr.map((label, value) => ({ value, label }));
console.log(res);

How would I convert this to a multi-dimensional array in javascript? [closed]

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 8 years ago.
Improve this question
How would I convert this table to a multi-dimensional array in javascript?
Use an object with the flag names as keys, and arrays of booleans as values.
var table = {
"STAT": [ false, false, false, true, true, false ]
// etc.
How about:
[
[ 'PATIENT_RESULT_IN_RANGE', [ 1, 0, 1 ] ],
[ 'QC_RESULT_IN_RANGE', [ 1, , 0, 1 ] ],
[ 'QC_DUE', [ 0, , , 1, 1, 1 ] ]
// ect.
]
or
{
'PATIENT_RESULT_IN_RANGE': [ 1, 0, 1 ],
'QC_RESULT_IN_RANGE': [ 1, , 0, 1 ],
'QC_DUE': [ 0, , , 1, 1, 1 ]
// ect.
}
I wouldn't use the stars (empty values) though...

Categories