Skip to content

Instantly share code, notes, and snippets.

@LucasCaixeta
Last active August 24, 2021 09:57
Show Gist options
  • Save LucasCaixeta/e8b931f624064ce9165cd1376df9e7a3 to your computer and use it in GitHub Desktop.
Save LucasCaixeta/e8b931f624064ce9165cd1376df9e7a3 to your computer and use it in GitHub Desktop.
import "./styles.css";
import styled from "styled-components";
const Product = styled.div`
display: flex;
border-style: dotted;
margin: 5px;
border: "1px solid black";
padding: 10px;
div {
margin: 5px;
}
span {
color: red;
font-weight: bold;
}
`;
export default function App() {
const array = [
"1,Adidas, 100, 40",
"2,Nike, 40, 10",
"3,Starbucks, 33, 75",
"4,Disney, 100, 40",
"5,Logitec, 74, 04",
"6,Apple, 85, 13"
];
interface ArrayProps {
id: string;
name: string;
promoted: number;
value: number;
}
const parsedItems = array.reduce<ArrayProps[]>((acc, item) => {
const [id,name,promoted,value] = item.split(",").trim();
return [
...acc,
{
id,
name,
promoted,
value
}
];
}, []);
console.log(parsedItems);
return (
<div className="App">
{parsedItems
.sort((a, b) => b.promoted - a.promoted)
.map(({ id, name, promoted, value }) => (
<Product key={id}>
<div>
<span>Name:</span> {name}
</div>
<div>
<span>Promoted:</span> {promoted}
</div>
<div>
<span>Value:</span> {value}
</div>
</Product>
))}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment