Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
render nested object after combining in react?

i want to render below JSON to the image shown

enter image description here

let finalSplit = [
    {
      start: "73",
      end: "76",
      splits: [
        {
          word: "Now",
          start: "73",
          color:"#FF6262",
          name:"extra",
          end: "76",
        },
      ],
    },
    {
      start: "27",
      end: "72",
      splits: [
        {
          word: "GitHub Actions",
          start: "31",
          name:"sub1",
          color:"#DFF652",
          end: "45",
        },
        {
          word: "the GitHub Actions “New Workflow” experience.",
          start: "27",
          name:"main",
          color:"#62FF8E",
          end: "72",
        },
        {
          word: "GitHub",
          start: "31",
          name:"sub2",
          color:"#9483FF",
          end: "37",
        },
      ],
    },
  ];
  

i tried to loop each array and render but since it format it have duplicate not able to render aggregated version

const Mark = () => {
    
    return (
        <>
        {
            finalSplit.map((i)=>{
                return (
                    <div>
                        i.split.map((j)=>{
                            <div>{j.text}</div>
                        })
                    </div>
                )
            })
        }
        <>
    );
}

need to some how generate other structure text : "announcing improvements to the GitHub Actions “New Workflow” experience. Now, when you want to create" and each place we get offsets also

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This one works to some extent with sorting and absolute positioning. If you had actual position values in pixels it will work as the image you have given.

const Mark = () => {
  const sortData = (data) => {
    return data.sort((a, b) => {
      if (a.start === b.start) {
        return b.end - a.end;
      } else {
        return a.start - b.start;
      }
    });
  };

  const sortNestedData = (data) => {
    const unsortedData = data.map((item) => {
      if (item?.splits) {
        return { ...item, splits: sortNestedData(item.splits) };
      }
      return item;
    });
    return sortData(unsortedData);
  };

  const renderNestedData = (data) => {
    const myData = data.map((item) => {
      return (
        <div
          style={{
            backgroundColor: item.color,
            position: "absolute",
            left: `${item.start}px`,
            // width: `${item.end - item.start}px`,
            top: "0px"
          }}
        >
          {item.word}
          {item.splits && renderNestedData(item.splits)}
        </div>
      );
    });
    return myData;
  };

  return (
    <div
      style={{
        backgroundColor: "lightblue",
        display: "flex",
        flexDirection: "row",
        position: "absolute"
      }}
    >
      {renderNestedData(sortNestedData(finalSplit))}
    </div>
  );
};

export default Mark;

Code sandbox => https://codesandbox.io/s/festive-violet-cc8s7?file=/src/App.js

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!