how to remove a record in an array or object by knowing the value of the record to be removed [closed] - javascript

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 5 years ago.
Improve this question
I have an Array as following
[
{ id: "172~~81259", title: "Absurdity (Theatre)" },
{ id: "2803~~281318", title: "Academic Administration" },
{ id: "3722~~296974", title: "Accessories Design" },
{ id: "23843", title: "Accommodation and Front Office Professional" },
{ id: "16834", title: "Accountant" },
{ id: "853~~64400", title: "Accounting Information System" },
{ id: "4403~~40149", title: "Activation and Experiential Advertising" },
{ id: "15~~39301", title: "Activation and Experiential Advertising" }
]
I want to filter(remove) already selected values from this array.so how can i filter by knowing the value only.

You need array.filter. It generates a new array containing only the items which cause the supplied callback to return true. Use that callback to check the value you want to check, whatever that value is.

Related

delete and modify properties in array inside json object [closed]

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 12 months ago.
Improve this question
Given the following JSON:
const myJson = {
id: 8,
active: true,
invoice: "2022/1001",
invoiceDate: "2022-02-02",
clientId: 1,
storeId: 1,
total: 76.95,
itens: [
{
id: 11,
quantity: 2,
price: 12.10,
total: 24.20,
productId: 1,
invoiceId: 8
},
{
id: 12,
quantity: 5,
price: 10.55,
total: 52.75,
productId: 2,
invoiceId: 8
}
]
};
I need a simple way to remove two attributes from the each item inside the 'itens' array, the properties are 'id' and 'invoiceId'. I also need to recalculate the 'total', coz I do not trust totally the source of the information, I rather multiply 'quantity' by 'price' myself.
I've produced this rather naive code:
myJson.itens.forEach(item => {
item.total =
item.quantity *
item.price;
delete item.id;
delete item.invoiceId;
});
And it works rather fine, however, no way that it must be it. Looks too lame and laborious to me. I'm exhausted googling for better ways of doing it.
Can it be done better?
Rather than mutating the original data, you could map it to a new object
const myJson = {"id":8,"active":true,"invoice":"2022/1001","invoiceDate":"2022-02-02","clientId":1,"storeId":1,"total":76.95,"itens":[{"id":11,"quantity":2,"price":12.1,"total":24.2,"productId":1,"invoiceId":8},{"id":12,"quantity":5,"price":10.55,"total":52.75,"productId":2,"invoiceId":8}]}
const newObject = {
...myJson, // keep the rest but map "itens"
itens: myJson.itens.map(({ id, invoiceId, ...iten }) => ({
...iten, // everything except "id" and "invoiceId"
total: iten.quantity * iten.price // recalc total
}))
}
console.log("newObject:", newObject)
.as-console-wrapper { max-height: 100% !important; }

Adding an incrementing number to each object in an array of objects with 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 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Say I have an array of objects:
const myArray = [{ color: 'red' }, { color: 'blue'}, { color: 'yellow'}]
How can I expand the object to add a key value pair to each element where the key is number and the value is a number that increments by one in each object? My desired outcome is:
const myNewArray = [{ color: 'red', number: 1 }, { color: 'blue', number: 2 }, { color: 'yellow', number: 3 }]
const myNewArray = myArray.map((item, index) => { return {"number" :index, ...item} } )

Store object values into an array [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 1 year ago.
Improve this question
I've this piece of code, it retrieves a list of 'Client' where FULL_NAME like a KEY I input.
[
{
id(pin):1
full_name(pin):"James Marius Bolord"
email(pin):"mariusjames32#gmail.com"
phone(pin):"9832910293"
dob(pin):"1981-05-05"
location(pin):"REXDALE"
user_id(pin):1
},
{
id(pin):23
full_name(pin):"Carl Barard"
email(pin):"mariusjames32#gmail.com"
phone(pin):"9832910293"
dob(pin):"1981-05-05"
location(pin):"REXDALE"
user_id(pin):1
},
{
id(pin):9
full_name(pin):"Tony Falape"
email(pin):"mariusjames32#gmail.com"
phone(pin):"9832910293"
dob(pin):"1981-05-05"
location(pin):"REXDALE"
user_id(pin):1
}
]
Now I learn that you can make use of object by doing this:
const zoo = {
lion: '🦁',
panda: '🐼',
};
Object.keys(zoo);
// ['lion', 'panda']
Object.values(zoo);
// ['🦁', '🐼']
Object.entries(zoo);
// [ ['lion', '🦁'], ['panda', '🐼'] ]
Until there, everything is fine on my mind. What I would like to do is having this type of result from that object:
const full_name = [
"James Marius Bolord",
"Carl Barard",
"Tony Falape",
];
So every time I type a key in, it should create an array of the full_name Key of the Client Table. I would appreciate the suggestion and ideas.
Ignoring the typo in your desired result, if all you want is to extract full names from your array of objects, then use Array.prototype.map:
const full_name = data.map(entry => entry.full_name);
If you want to implement some kind of autocomplete/autosuggest feature, <datalist> is a very good candidate for your use-case:
const data = [{
id: 1,
full_name: "James Marius Bolord",
email: "mariusjames32#gmail.com",
phone: "9832910293",
dob: "1981-05-05",
location: "REXDALE",
user_id: 1
},
{
id: 23,
full_name: "Carl Barard",
email: "mariusjames32#gmail.com",
phone: "9832910293",
dob: "1981-05-05",
location: "REXDALE",
user_id: 1
},
{
id: 9,
full_name: "Tony Falape",
email: "mariusjames32#gmail.com",
phone: "9832910293",
dob: "1981-05-05",
location: "REXDALE",
user_id: 1
}
];
const full_name = data.map(entry => entry.full_name);
console.log(full_name);
// Populate datalist as a UI/UX feature
const datalist = document.querySelector('#fullnames');
data.forEach(entry => {
const option = document.createElement('option');
option.value = entry.full_name;
datalist.appendChild(option);
});
<input list="fullnames" />
<datalist id="fullnames"></datalist>

How to iterate over these array objects? [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 2 years ago.
Improve this question
let arr = [{
text: "first",
nodes: [{
text: "a1"
}, {
text: "a2"
}]
},
{
text: "Second",
nodes: [{
text: "b1"
}]
}, {
text: "Third",
nodes: [{
text: "c1"
}]
}
]
arr.map((el, index) => {
console.log(el.nodes[index])
})
How can I iterate over all the nodes which has different size of array objects.
I've tried iterating over them, but the index keeps incrementing and skip past the nodes
You can loop in a loop... like:
let answerObj=[];
arr.forEach(item=>{
let nodeList=[];
item.nodes.forEach(el => {
nodeList.push(el)
})
answerObj.push({ text: item.text, nodes: nodeList})
})
console.log(answerObj)
And this is what you get: Output
And you can output by [index] as well if you want.

How to create array out of object entries? [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 2 years ago.
Improve this question
I need help creating a JavaScript object:
I have three fields from the input form in React state object:
{
calories: "45",
fats: "56",
proteins: "67"
}
I would like to create a new array i.e. nutrientInformation from this data. The output should resemble this:
nutrientInformation: [
{
calories: 45
},
{
fats: 56
},
{
proteins: 67
}
]
const state = { calories: "45", fats: "56", proteins: "67" };
const nutrientInformation = Object.entries(state).map(([key, value]) => ({
[key]: value
}));
console.log(nutrientInformation);

Categories

Resources