By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to find a property of one object by matching a different property to another object
JavaScript

Javascript – How to find a property of one object by matching a different property to another object

Admin
Last updated: 2023/12/24 at 5:59 AM
Admin
Share
2 Min Read
How to find a property of one object by matching a different property to another object

Problem:

I have two objects in a d3 construct:

Contents
Problem:Solution:
const visitors = [
              {
                 name: "polygon 1",
                 total: 342,
                 color: "red"
              },
              {
                 name: "polygon 2",
                 total: 342,
                 color: "blue"
              },
              {
                 name: "polygon 3",
                 total: 342,
                 color: "green"
              }
            ]

const polys = [{
                 name: "polygon 1",
                 points:[
                    {"x":-25, "y":48},
                    {"x":3,"y":48},
                    {"x":3,"y":40},
                    {"x":-25,"y":40},
                    {"x":-25,"y":48}
                  ]
               },
               {
                  name: "polygon 2",
                  points:[
                      {"x":5, "y":40},
                      {"x":23,"y":40},
                      {"x":23,"y":18},
                      {"x":5,"y":18},
                      {"x":5, "y":40}
                    ]
                },
                {
                    name: "polygon 3",
                    points:[
                      {"x":-25, "y":40},
                      {"x":5,"y":40},
                      {"x":5,"y":34},
                      {"x":-25,"y":34},
                      {"x":-25, "y":40}
                    ]
               }]

Given the name of the polygon from the ‘polys’ object I need to find the color for that polygon from the ‘visitors’ object.

I’ve tried:

d = one object of the 'polys' obj

.attr("fill", function(d, visitors) {
    for(var key in d){
        if (key === 'name' && d.name === visitors.name){
            return visitors.color;
        }
    }
});

I have no problem returning the color if it was in the ‘polys’ object which defines the polygon, but the ‘polys’ object will essentially be static and the ‘visitors’ object will be dynamic and the color will change based on the number of visitors. How do I return the color from the ‘visitors’ object?

Solution:

I actually got the answer from chatGPT:

// Select your polygons and set the fill color based on the 'visitors' data
d3.select("your-polygon-selector")
    .attr("fill", function (d) {
        const name = d.name; // Get the name of the current polygon
        // Find the corresponding visitor object based on the polygon name
        const visitor = visitors.find(visitor => visitor.name === name);
        if (visitor) {
            return visitor.color; // Set the fill color from the visitor object
        } else {
            // Handle the case where the visitor data is not found for the polygon
            return "default-color"; // You can set a default color here
        }
    });

This worked.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article I have duplicate child elements in my RSS feed. Won't parse it in JavaScript Javascript – I have duplicate child elements in my RSS feed. Won’t parse it in JavaScript
Next Article Problems with data persistence in the state and token loss Javascript – Problems with data persistence in the state and token loss
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?