logo
Tags down

shadow

Filter array property of object without mutating the object itself


By : ptaczek
Date : October 17 2020, 08:10 PM
it fixes the issue You can filter() the array, and then merge it with the original object to create a new one without mutating the original. Something like:
code :
let myObject = {
    propA: [1,2,3],
    propB: "foo"
}
let propA = myObject.propA.filter(el => el === 2)
let newobj = {...myObject, propA}

// new Object
console.log("New:", newobj)

// old object unchanges
console.log("Old:", myObject)


Share : facebook icon twitter icon

Filter object array to return the object with the latest date property


By : haxhi daci
Date : March 29 2020, 07:55 AM
hope this fix your issue You should use sort function:
without null checking you can simply use
code :
array.sort(function(a,b) { 
    return new Date(b.date).getTime() - new Date(a.date).getTime() 
})[0];

Filter array of object by nested array of object boolean property


By : Ashvin Rajani
Date : March 29 2020, 07:55 AM
it should still fix some issue Single filter() is enough here. Since the visible property in item is at 0 index you can use the that index:
code :
const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"}]}];

const filteredPerson = Person.filter(person => person.items[0].visible)
console.log(filteredPerson);
const Person = [{firstname: "john",lastname: "doe",items: [{visible: true,foo: "bar"}]},{firstname: "jane",lastname: "doe",items: [{visible: false,foo: "bar"}]},{firstname: "john",lastname: "adam",items: [{visible: true,foo: "bar"}]}];

const filteredPerson = Person.filter(person => person.items.some(s =>s.visible)).map(p => ({items: p.items}));
console.log(filteredPerson);

Filter an array of objects by the property inside an object contained in an array inside the object iterated [Javascript


By : Amr Essam
Date : March 29 2020, 07:55 AM
this one helps. You were very close to correct with the second try. If you change it from forEach to find and return the value, it will work.
code :
const videos = [
  {
    name: 'video name',
    url: 'video url',
    tags: [
      {
        tag: 'reels',
      },
      {
        tag: 'demos',
      },
    ]
  },
  {
    name: 'video name2',
    url: 'video url2',
    tags: [
      {
        tag: 'demos',
      },
    ]
  },
];

const filtered = videos.filter(video => {
  return video.tags.find(tag => {
    if (tag.tag === 'reels') {
      return true;
    };
  });
});

console.log(filtered);

// shortened to one line
const filtered2 = videos.filter(video => video.tags.find(tag => tag.tag === 'reels'));

console.log(filtered2);

how to filter array object using object property


By : Mark
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I got an array of object , You forget to mentioned return while filtering the data inside map
code :
return o[k].map(pay => {
  if(pay.label.toLowerCase().includes(string.toLowerCase()))
    return pay;
const devices = [{
    deviceLocation: {
      label: "Pure",
      value: "pure"
    },
    deviceName: "test7",
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    }, {
      label: "cd",
      value: "cd"
    }, {
      label: "ef",
      value: "ef"
    }],
    deviceType: "iPad",
    id: "001",
    connected: false,
    enabled: false,
  },
  {
    deviceLocation: {
      label: "kandy",
      value: "kandy"
    },
    deviceName: "test4",
    devicePaymentMethods: [{
      label: "ab",
      value: "ab"
    }, {
      label: "cd",
      value: "cd"
    }],
    deviceType: "iPad",
    id: "004",
    connected: false,
    enabled: false,
  }
];


const filterByValue = (array, string) => {
  return array.filter(o =>
    Object.keys(o).some(k => {
      if (k === "deviceName") {
        return o[k].toLowerCase().includes(string.toLowerCase());
      } else if (k === "deviceLocation") {
        return o[k].label.toLowerCase().includes(string.toLowerCase());
      } else if (k === "devicePaymentMethods") {
        return o[k].some(pay => {
          if (pay.label.toLowerCase().includes(string.toLowerCase())) {
            return pay;
          }
        });
      }
    })
  );
};

console.log(filterByValue(devices, "ab"));
console.log(filterByValue(devices, "ef"));

Objective C- Adding an object to an array, whilst mutating that object


By : Itis Itis
Date : March 29 2020, 07:55 AM
hop of those help? Yes, it's safe. The array only cares about storing a reference to the object. Any changes to the object itself are totally invisible to the array. Of course if one thread reads myObject = myArray [i] then that thread must be aware that the contents of the object could change at any time.
Related Posts Related Posts :
  • Session.cookies.get_dict() return an empty dictionary
  • Performing logical operation inside Higher Order Component on prop set
  • how to force the browser to run the javascript every time it called
  • REGEX get all content after a series of special characters
  • deduce amount issue, not the same value foreach execute
  • Regex to get individual function bodies from group of functions
  • Stop animation from executing if coming from an internal link
  • How to retrieve total of "else if"
  • Multiple JS slideshows on one page
  • Importing async functions in Javascript
  • How to handle state on array of checkboxes?
  • React Native - Redux ~ Props updating when not getting called
  • Fast way to add elements into an Uint8ClampedArray typed array
  • How to retrieve values from a checkbox group generated dinamically with jquery when page loads
  • Progress bar between two dates (fetch "var" from script), HTML
  • Why do Every time I start my function I get NaN
  • Angular routing change in url
  • Javascript - Change symbol on embedded tradingview script.text
  • How to select specific option?
  • Checking booleans from multiple arrays simultaneously, without multiple if statements
  • How to get a subarray?
  • Javascript JSON woes
  • Use Jquery animate to have a button move a box to the next corner
  • Why is this JavaScript not interpreted as a code block when semi-colon is used?
  • Reload another page when I click on button
  • Should functions that call a callback function be bound?
  • For loop print in single line separated by spaces - JS
  • Add Javascript Variable into asp-route-data
  • Two different POST requests are making use of the same variable
  • Convert JSOn object into a flat JSON Array
  • Manually adding to an HTML range input gives unexpected results
  • Refreshing component after making api call Reactjs
  • Google App Script : Finding The Last Blank Row
  • I cant for the life of me figure out whats wrong with this script
  • Order divs by ID in Javascript
  • set new element class property value
  • How to pass a java variable to a different jsp page containing javascript?
  • Locked it method in chai
  • React JS : history.push is not a function error and it isn't navigating to a different page onclick of swal
  • How can i set timeout for localstorage for Angular 2+?
  • Must use destructuring props assignment
  • Jest Compared values have no visual difference. when testing arrays
  • Uncaught Error: #90211 ExtensionName Error - Node.JS, Heroku, JavaScript App using FusionCharts
  • Is it possible in firebase auth to update user and custom claims at the same time node?
  • Filter out capitalized letters
  • Yet Another Google Firebase Error "Function returned undefined, expected Promise or value"
  • How do I address method in a Vue.js component from inside callback function?
  • What is the name of this feature in JavaScript?
  • Vue js using scope-slot and trying to use a property from within but having no visibility
  • Dynamic javascript table won't display iterations from array
  • How to start/end smoke.js animation by clicking a nav button?
  • Map array of strings as object properties
  • Appending new inputs with JS wipes previous ones
  • Chai Request With URL instead of JS File
  • React Jest Async Tests
  • Unordered list bullet not showing at an expected location
  • How to create an array from an object?
  • How to get prefix name from html tag
  • javascript - Simulate a click event (tap with finger) on a button in iOS
  • How to get the value of checkboxes in a kendo grid
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org